?
Solved

menu in master page

Posted on 2011-09-09
1
Medium Priority
?
346 Views
Last Modified: 2012-05-12
hey guys i have a asp menu in my master page, my problem is setting the item to be selected when i click from page to page?

like i have a default page and master

my menu is in the master page

please help?

i am coding in c#
0
Comment
Question by:JCWEBHOST
[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
1 Comment
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 2000 total points
ID: 36510704
Here is a rough example on how you could do it.

Lets say you have a master page that looks like this:
 
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>

<!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>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ul id="menu">
            <li><asp:LinkButton ID="LinkButton1" runat="server" OnClick="MenuNavigation_Clicked" Text="Link 1" /></li>
            <li><asp:LinkButton ID="LinkButton2" runat="server" OnClick="MenuNavigation_Clicked" Text="Link 2" /></li>
            <li><asp:LinkButton ID="LinkButton3" runat="server" OnClick="MenuNavigation_Clicked" Text="Link 3" /></li>
            <li><asp:LinkButton ID="LinkButton4" runat="server" OnClick="MenuNavigation_Clicked" Text="Link 4" /></li>
        </ul>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>        
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void MenuNavigation_Clicked(object sender, EventArgs e)
        {
            LinkButton selectedLink = (LinkButton)sender;

            switch (selectedLink.Text)
            {
                case "Link 1": Response.Redirect("somepage.com"); break;
                case "Link 2": Response.Redirect("somepage.com"); break;
                case "Link 3": Response.Redirect("somepage.com"); break;
                case "Link 4":
                default: Response.Redirect("somepage.com"); break;
            }
        }
    }
}

Open in new window


Then on each content page you can access the controls on the master page like so:
 
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)(this.Master.FindControl("LinkButton1"));
            link.Attributes.Add("style", "color: red");
        }
    }
}

Open in new window


So basically when you need to access a control on a master page from a content page you use: this.Master.FindControl("control-id")
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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