Dynamically change tag attribute

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
 
M

Mark Rae [MVP]

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
 
J

John

Perfect. Many thanks Mark, I was hoping there would be an easier solution
like this.

Best regards

John
 
J

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
 
M

Mark Rae [MVP]

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";
 
J

John

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
 
M

Mark Rae [MVP]

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top