Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Dynamically change tag attribute

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 14 Dec 2007 20:13 GMT
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
Mark Rae [MVP] - 14 Dec 2007 20:33 GMT
> 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

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

John - 14 Dec 2007 22:25 GMT
Perfect.  Many thanks Mark, I was hoping there would be an easier solution
like this.

Best regards

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
[quoted text clipped - 14 lines]
>
> http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
John - 17 Dec 2007 15:03 GMT
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

>> 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
[quoted text clipped - 14 lines]
>
> http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
Mark Rae [MVP] - 17 Dec 2007 15:24 GMT
>>> 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
[quoted text clipped - 19 lines]
> 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";

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

John - 17 Dec 2007 16:09 GMT
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

>>>> 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
[quoted text clipped - 29 lines]
>
> ((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
Mark Rae [MVP] - 17 Dec 2007 16:14 GMT
>> ((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
>
[quoted text clipped - 3 lines]
>
> Does this also work for plain old HTML tags as well?

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

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

John - 17 Dec 2007 17:23 GMT
Great.  Thanks very much for your help Mark.

best regards

John

>>> ((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
>>
[quoted text clipped - 5 lines]
>
> Yep - just give them an ID and mark them runat="server"

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.