Link to home
Start Free TrialLog in
Avatar of wizkid2332
wizkid2332

asked on

Altering a master page from another page n C#

In my master page I have code that creates tabs (like a menu), For example:
<ul id="primary">
     <li><span>Home</span></li>
     <li><asp:HyperLink ID="HyperLink1" NavigateUrl="~/signIn.aspx" runat="server">Sign In</asp:HyperLink></li>
     <li><asp:HyperLink ID="HyperLink2" NavigateUrl="~/register.aspx" runat="server">New User</asp:HyperLink></li>
      <li><asp:HyperLink ID="HyperLink3" NavigateUrl="~/contactUs.aspx" runat="server">Contact Us</asp:HyperLink></li>
</ul>

I have this in a table.  Basically, the default home page has the HOME menu option highlighted (in a css flle I have the span tag a different color).  When I click on the other pages, I go to them, but the HOME page option is always highlighted.  How can I alter the master page in the other pages to get the menu to correctly highlight the current page it is in?
Avatar of wht1986
wht1986
Flag of United States of America image

I try to expose what i need on the master page file via properties or a public methods. In the example below, I have a simple asp.net menu control on the master page and 2 pages which use it. I have a public method on the master page that allows me to set the selected menu item. Then on an page load I can call the master page's method.
Avatar of David H.H.Lee
Hi wizkid2332,
Probably you can make "Home" control as part of server control instead. Then, bind the different CSS class based on clicked page, each menu'll be highlighted accordingly based on current requested URL in master page.
eg:

aspx
=======
<ul id="primary">
     <li><div id="divHome" runat="server">Home</div></li>
     <li><asp:HyperLink ID="HyperLink1" NavigateUrl="~/signIn.aspx" runat="server">Sign In</asp:HyperLink></li>
     <li><asp:HyperLink ID="HyperLink2" NavigateUrl="~/register.aspx" runat="server">New User</asp:HyperLink></li>
      <li><asp:HyperLink ID="HyperLink3" NavigateUrl="~/contactUs.aspx" runat="server">Contact Us</asp:HyperLink></li>
</ul>

c# code behind
================
Page Load
------------------
        if (Page.Request.Url.ToString.IndexOf("Home.aspx") != -1){
            divHome.Attributes.Add("class", "HomeHighlight");//css classname
        }else{
            divHome.Attributes.Add("class", "HomeNormal");//css classname
        }

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wizkid2332
wizkid2332

ASKER

Thanks, this makes a lot more sense.  I have it working the way I want now! :)