[RESOLVED] Add 'class' attribute to html controlat page load
I have a set of links that exist in the master page. I have a css class i would like to assign to a particular one of them depending on if that is the page you are at during page load. ex: Home will have the current_page_item class if you are at the index page
This is what I have:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DirectCast(Me.Master.FindControl("ctl00_homeLi"), HtmlGenericControl).Attributes.Add("class", "'current_page_item'")
End Sub
The problem is it does not seem to work. Any Ideas on what I may be doing wrong?
Re: Add 'class' attribute to html controlat page load
Hey,
Can you shed some light on what you mean by:
Quote:
The problem is it does not seem to work
Do you get an exception? If so, what is it? Have you stepped through your code in the debugger, and watched what is happening?
At a guess, I would say that FindControl isn't finding the control in question, what without more information, it is difficult to say.
Depending on where in the Master Page this control is located, you might have to recurse to find it, you can find an example of this here:
http://www.west-wind.com/Weblog/posts/5127.aspx
Gary
Re: Add 'class' attribute to html controlat page load
Sorry for the delay here.
What i am trying now is this:
VB
Code:
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim homeLi As New HtmlGenericControl
homeLi = DirectCast(Me.Master.FindControl("ctl00_homeLi"), HtmlGenericControl)
homeLi.Attributes.Add("class", "current_page_item")
'homeLi.Style.Add("text-decoration", "underline")
'homeLi.Style.Add("color", "#FFFFFF")
End Sub
CSS:
Code:
#menu .current_page_item a {
text-decoration: underline;
color: #FFFFFF;
}
I have tried attributes.add to reference the CSS file, and to add styles, but i always get a nullreferenceexception on the first line of homeLi.attributes.add
Is there a place where I can see what is contained within the attributes dictionary or what I may do to get this working?
Re: Add 'class' attribute to html controlat page load
Hey,
A null reference exception would suggest that the following line is failing to return a control:
Code:
homeLi = DirectCast(Me.Master.FindControl("ctl00_homeLi"), HtmlGenericControl)
This would suggest that FindControl can't find something called "ctl00_homeLi". To me, this looks like the ClientID of the control, or do you actually have a control with the ID of "ctl00_homeLi".
Gary
Re: Add 'class' attribute to html controlat page load
The control's ID at design time is homeLi, but at run time, it becomes ct100_homeLi. I guess i figured it was finding the control because the null reference was on the next line. I thought that meant there was an issue with the collection of attributes.
Re: Add 'class' attribute to html controlat page load
I figured it out. I removed my runat='server' from the control. I then referenced it by its design time ID, homeLi. It's ID still changed to ct100_homeLi at run time, but I did not get any issue with referencing it and assigning it that CSS class worked.
As usual, Thanks for all of your help Gary.
Re: [RESOLVED] Add 'class' attribute to html controlat page load
Hey,
Yes, this is normal, there is both a ID and ClientID. The ID is what you set in the designer, when you are creating the control, i.e. homeLi, but the ClientID is something that ASP.Net will create for you at runtime. Why I hear you ask?
Well, imagine you have a Repeater/GridView control, that has multiple rows, each with one of these controls one it. On the client, each of these controls needs to have a unique ID, so ASP.Net, appending some other information, to create the ClientID, for instance, the name of the containing control, or a unique portion, such as ct100_homeLi.
I am curious though, by removing the runat="server" attribute, you are essentially removing the ability for that control to be found in the server side code, you shouldn't have had to do that.
Gary
Re: [RESOLVED] Add 'class' attribute to html controlat page load
As it turns out, I still do need the runat="server" in the Li element. The change that was necessary was to reference the control by it's design time name. What confused me here in the first place was that a while back i wrote some javascript and had to reference controls by their run time name rather than their design time name.
Re: [RESOLVED] Add 'class' attribute to html controlat page load
Hey,
But this is the distinction. On the cient side, in javascript code, you have to reference controls using their unique ClientID. On the server side, in C# or vb.net code, you use the ID, in the context of a particular row, or containing control. This is why you will find FindControl on lots of objects. It finds the control you are looking for within the context that you specify, so you don't need the ClientID, and in actual fact means nothing on the server.
Gary