Solved

menu in master page

Posted on 2011-09-09
1
338 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
1 Comment
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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