473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically edit class of element in Master Page

CJM
I have a site that is being built using master pages, and includes a menu
that I want to manipulate dynamically.

Specifically the currently-selected menu option is styled differently to the
others, as follows:

<div id="menu">
<ul>
<li class="current_page_item"><a href="/">Home</a></li>
<li><a href="/about/">About Us</a></li>
<li><a href="/services">Services</a></li>
<li><a href="/projects/">Projects</a></li>
<li><a href="/events/">Events</a></li>
<li><a href="/contact/">Contact Us</a></li>
</ul>
</div>

I want to be able to move the class from the first item, and apply it to the
oppropriate menu item for each page, during the Form_Load() event.

I know I could place the entire menu into it's own Content Placeholder, but
a) I thought doing it dynamically might be more sophisticated, and b) I just
want to know how to do it for the hell of it - it's a learning exercise.

I know I need to use FindControl. and I know there are complications
regarding naming containers, but I haven't managed to find or figure out the
right solution.

Thanks

Chris

Jun 1 '08 #1
4 5649
Hi, this will work for you

//HTML
<ul>

<li id="li1" runat="server">Menu Item1</li>

<li id="li2" runat="server">Menu Item2</li>

</ul>

//Page Lload

System.Web.UI.HtmlControls.HtmlGenericControl li;

li = (System.Web.UI.HtmlControls.HtmlGenericControl)
this.Master.FindControl("li1");

li.Attributes.Add("class", "MySelectedClass.css");

"CJM" <cj*******@nospam.yahoo.co.ukwrote in message
news:FB**********************************@microsof t.com...
>I have a site that is being built using master pages, and includes a menu
that I want to manipulate dynamically.

Specifically the currently-selected menu option is styled differently to
the others, as follows:

<div id="menu">
<ul>
<li class="current_page_item"><a href="/">Home</a></li>
<li><a href="/about/">About Us</a></li>
<li><a href="/services">Services</a></li>
<li><a href="/projects/">Projects</a></li>
<li><a href="/events/">Events</a></li>
<li><a href="/contact/">Contact Us</a></li>
</ul>
</div>

I want to be able to move the class from the first item, and apply it to
the oppropriate menu item for each page, during the Form_Load() event.

I know I could place the entire menu into it's own Content Placeholder,
but a) I thought doing it dynamically might be more sophisticated, and b)
I just want to know how to do it for the hell of it - it's a learning
exercise.

I know I need to use FindControl. and I know there are complications
regarding naming containers, but I haven't managed to find or figure out
the right solution.

Thanks

Chris

Jun 1 '08 #2
CJM

"Rain" <me@myplace.comwrote in message
news:ug**************@TK2MSFTNGP02.phx.gbl...
Hi, this will work for you

//HTML
<ul>

<li id="li1" runat="server">Menu Item1</li>

<li id="li2" runat="server">Menu Item2</li>

</ul>

//Page Lload

System.Web.UI.HtmlControls.HtmlGenericControl li;

li = (System.Web.UI.HtmlControls.HtmlGenericControl)
this.Master.FindControl("li1");

li.Attributes.Add("class", "MySelectedClass.css");

That's precisely the approach I was trying to take (albeit using VB.Net):

Dim oCtl As System.Web.UI.HtmlControls.HtmlGenericControl
oCtl = Page.Master.FindControl("menu_home")
With oCtl
.Attributes.Add("class", "current_page_item")
End With

Unfortunately, I'm getting 'Object reference not set to an instance of an
object'....

So, my FindControl call is not hitting the right target... Where am I going
wrong?

Cheers

Chris

Jun 1 '08 #3
whats oCtl = Page.Master.FindControl("menu_home")

Menu home didnt appear in your html text at all.
"CJM" <cj*******@nospam.yahoo.co.ukwrote in message
news:FB**********************************@microsof t.com...
>
"Rain" <me@myplace.comwrote in message
news:ug**************@TK2MSFTNGP02.phx.gbl...
>Hi, this will work for you

//HTML
<ul>

<li id="li1" runat="server">Menu Item1</li>

<li id="li2" runat="server">Menu Item2</li>

</ul>

//Page Lload

System.Web.UI.HtmlControls.HtmlGenericControl li;

li = (System.Web.UI.HtmlControls.HtmlGenericControl)
this.Master.FindControl("li1");

li.Attributes.Add("class", "MySelectedClass.css");


That's precisely the approach I was trying to take (albeit using VB.Net):

Dim oCtl As System.Web.UI.HtmlControls.HtmlGenericControl
oCtl = Page.Master.FindControl("menu_home")
With oCtl
.Attributes.Add("class", "current_page_item")
End With

Unfortunately, I'm getting 'Object reference not set to an instance of an
object'....

So, my FindControl call is not hitting the right target... Where am I
going wrong?

Cheers

Chris

Jun 1 '08 #4
CJM

"Rain" <me@myplace.comwrote in message
news:eQ*************@TK2MSFTNGP04.phx.gbl...
whats oCtl = Page.Master.FindControl("menu_home")

Menu home didnt appear in your html text at all.

I simplied my original code snippet... I'd actually tried roughly what you
were suggesting, but it hadn't worked. Actually tried a few other variations
too, but since I didn't know which one was heading in the right direction, I
just reverted to basics.

So my current snippets would be:

<div id="menu">
<ul>
<li id="menu_home" runat="server"><a href="/">Home</a></li>
<li id="menu_about" runat="server"><a href="/about/">About Us</a></li>
<li id="menu_services" runat="server"><a
href="/services">Services</a></li>
<li id="menu_projects" runat="server"><a
href="/projects/">Projects</a></li>
<li id="menu_events" runat="server"><a href="/events/">Events</a></li>
<li id="menu_contact" runat="server"><a href="/contact/">Contact
Us</a></li>
</ul>
</div>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim oCtl As System.Web.UI.HtmlControls.HtmlGenericControl
oCtl = Page.Master.FindControl("menu")
With oCtl
.Attributes.Add("class", "current_page_item")
End With
End Sub

And in case it isn't clear, 'current_page_item' is the class I wish to
assign to the active menu item...

Chris

Jun 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert | last post by:
Are there any other ways to dynamically apply CSS styling to a page (without using inline CSS)? I'm sure I could dynamically generate a new and uniquely named CSS file "on the fly" when users...
3
by: Ron | last post by:
Hi all, I've come across a problem when casting user controls in ASP.NET 2.0, hopefully someone can help. I have a master page with a user control on it (myMenu) that I wish to programmatically...
3
by: Wouter | last post by:
Hi All, I am loading a web user control (.NET 2.0) into a placeholder on a content page ( plus the use of a master page). The controls is a list of companie, one you select a company for...
2
by: Mark Rae | last post by:
Hi, It's easy enough to get a content page to refer to public properties on its associated master page e.g. public partial class master_secure : System.Web.UI.MasterPage { public Label...
1
by: Sam | last post by:
Attached I am sending 2 URL's from MSFT ASP.net Quick Start Tutorial Web Site. 1) Run it URL: http://www.asp.net/QuickStart/aspnet/samples/data/GridViewMasterDetai... 2) View Source URL:...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
23
by: LvBohemian | last post by:
I am playing around with creating some menus dynamically, and they create fine and show up ok when I want them to; but when I select a menu NavigateUrl at run time, the applicable url shows up...
5
by: CJM | last post by:
I have a site that is being built using master pages, and includes a menu that I want to manipulate dynamically. Specifically the currently-selected menu option is styled differently to the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.