Solved

menu in master page

Posted on 2011-09-09
1
326 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now