Dynamically change tag attribute

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi there,

I have a CSS based menu in a master page that uses class attributes to
determine which item is currently selected. So, for example if the Homepage
is currently the active page then its list item tag carries a class
attribute of "current", while all other items carry one of "select".

What I want to know is what is the best approach to change these attributes
based on the current content page. Should I be changing the attribute in
the pre-render event using HTMLTextWriter or would some kind of client-side
script work better here?

All advice is welcome! What's best practice?

Thanks

John
 
What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some kind
of client-side script work better here?

No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
 
Perfect. Many thanks Mark, I was hoping there would be an easier solution
like this.

Best regards

John
 
Hello Mark,

I've got the property declared in the MasterPage class, but can you give me
hint on how to set the various <ul> tags' attributes. I'm giving them
unique IDs so can I reference them directly in some way or do I have to walk
through the DOM checking for each one (I'm guessing not)?

Thanks

John
 
I've got the property declared in the MasterPage class, but can you give
me hint on how to set the various <ul> tags' attributes. I'm giving them
unique IDs so can I reference them directly in some way or do I have to
walk through the DOM checking for each one (I'm guessing not)?

This is explained in the 4Guys article.

Alternatively, you can use FindControl which achieves the same effect,
though purists would no doubt say that it's not very OOP... ;-)

E.g. to set the text in a TextBox control on the MasterPage from the content
page, you could do something like:

((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
 
Hi Mark,

Thanks for this. I did read the article, which was very useful, but from my
first reading this only applied to server controls (also the FindControl
method?).

Does this also work for plain old HTML tags as well?

Apologies if I'm being slow on this......

Best regards

John
 
Thanks for this. I did read the article, which was very useful, but from
my first reading this only applied to server controls (also the
FindControl method?).

Does this also work for plain old HTML tags as well?

Yep - just give them an ID and mark them runat="server"
 
Back
Top