Solved

changing "class="" " through code behind aspx vb.net

Posted on 2009-04-06
9
1,470 Views
Last Modified: 2012-05-06
Hi all

I want to change the class attribute on the below through code behind on page load:

<a href="" class="current" id="one" runat="server"></a>
<a href="" class="" id="two" runat="server"></a>

I want to change 'one's class to nothing and 'two's class to current. How would I do this through vb.net?

Thanks!
0
Comment
Question by:garethh86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24077537
Just need to use these two lines:
    Me.one.CssClass = "";
    Me.two.CssClass = "current";

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24078220
Whilst what you have works, I would recommend using the asp.net tags for this (primarily for consistency in naming).

Basically what oobayly said, but remember that in VB.NET the semi-colon is not used to separate statements.
Page Itself (.aspx):
====================
 
<asp:Label ID="one" runat="server" NavigateUrl="" CssClass="current"/>
<asp:Label ID="two" runat="server" NavigateUrl="" CssClass=""/>
 
 
Code Behind Page (.aspx.vb):
============================
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
     Me.one.CssClass = ""
     Me.two.CssClass = "current";
 
End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24078241
Whoops, somehow I managed to paste the semi.....shows how easily it is done!
Page Itself (.aspx):
====================
 
<asp:Label ID="one" runat="server" NavigateUrl="" CssClass="current"/>
<asp:Label ID="two" runat="server" NavigateUrl="" CssClass=""/>
 
 
Code Behind Page (.aspx.vb):
============================
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
     Me.one.CssClass = ""
     Me.two.CssClass = "current"
 
End Sub

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 15

Expert Comment

by:oobayly
ID: 24078286
@numberkruncher
Oops, force of habit :-), though it's nice to see I'm not the only one!
0
 
LVL 7

Author Comment

by:garethh86
ID: 24079998
I get the error:

cssclass is not a member of htmlcontrol.htmlanchor

I was looking along the lines of re-writing the innerHTML on the anchor elements but could not get it working, I also need this to work for other elements on the page so although I could replace these with labels I will still need another way of doing this.

Any other ideas? Thanks for your help so far!

Gareth
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24080021
Try using the
0
 
LVL 7

Author Comment

by:garethh86
ID: 24080169
I can replace  the anchor with an asp.net tag but I need this to work with other html elements aswell, let me re-phrase the question to explain:

<div ID="Div1" runat="server" class="current"></div>
<div ID="Div2" runat="server" class=""></div>

How would I change the class using vb.net on pageload? Not only limited to this element, I have lots of different elements I want to apply different classes to depending on the page it is on.

From what I could find I need to re-write the innerHTML of the element simply using string.replace but I cant get it working, unless of course you know of another way around it. cssclass is not a member of htmlcontrol.html<anyelement>.

Thanks

Gareth
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24080332
You may consider using asp.net processing instructions, they happen on load, and you can use inline VB.NET code. So you can access all of your pages properties and such.

Btw, asp:Panel can be used in place of div.
<div ID="Div1" runat="server" class="<%= "current" %>"></div>
<div ID="Div2" runat="server" class="<%= "" %>"></div>

Open in new window

0
 
LVL 7

Accepted Solution

by:
garethh86 earned 0 total points
ID: 24081111
Found it, can be used on any html element so long as you specify an ID and runat="server" on the tag
YourElement.Attributes.Add("class", "myCssClass")
YourElement.Attributes.Add("width", "100px")
YourElement.Attributes.Add("border", "1px")
YourElement.Attributes.Add("alt", "Alternative Text for Images")
 
YourElement.Attributes.Remove("class")
YourElement.Attributes.Remove("border")

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question