Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

menu in master page

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
JCWEBHOST
Asked:
JCWEBHOST
1 Solution
 
P1ST0LPETECommented:
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: 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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now