Solved

Altering a master page from another page n C#

Posted on 2009-04-14
4
367 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:wizkid2332
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24144456
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.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24144470
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
        }

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 250 total points
ID: 24144476
Hmm doesnt seem like my file attachment worked. Here are the code snippets

MyMasterPage.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MyMasterPage.master.cs" Inherits="MyMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title></head>
<body>
    <form id="form1" runat="server">
        <asp:Menu ID="Menu1" runat="server" StaticSelectedStyle-BackColor="#DDFF44" Orientation="Horizontal">
            <Items>
                <asp:MenuItem Text="Default" Value="Default" NavigateUrl="~/Default.aspx" Selected="true" />
                <asp:MenuItem Text="Other" Value="Other" NavigateUrl="~/Other.aspx" />                
            </Items>
        </asp:Menu>
        <p><asp:Button ID="Button1" runat="server" Text="Force a postback" /></p>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server" />
    </form>
</body>
</html>

MyMasterPage.master.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class MyMasterPage : System.Web.UI.MasterPage
{
    public void SetSelectedMenu(string value)
    {
        Menu1.FindItem(value).Selected = true;
    }

}

Default.aspx

<%@ Page Language="C#" MasterPageFile="~/MyMasterPage.master" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">Default Page</asp:Content>

Other.aspx

<%@ Page Language="C#" MasterPageFile="~/MyMasterPage.master" AutoEventWireup="true" CodeFile="Other.aspx.cs" Inherits="Other" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">Other Page</asp:Content>

Other.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Other : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            MyMasterPage mmp = (MyMasterPage)this.Master;
            mmp.SetSelectedMenu("Other");
        }
    }
}

0
 

Author Closing Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replace value 2 35
Hide and Unhide Table 6 27
ASP.NET data base connection 35 43
Adjust the codes 3 36
Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question