Link to home
Start Free TrialLog in
Avatar of garethh86
garethh86Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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!
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Open in new window

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

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

@numberkruncher
Oops, force of habit :-), though it's nice to see I'm not the only one!
Avatar of garethh86

ASKER

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
Try using the
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
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

ASKER CERTIFIED SOLUTION
Avatar of garethh86
garethh86
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial