473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5648
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.