Solved

menu in master page

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS question 16 63
Split on ASP 8 53
Data Analysis 7 59
Pull data from another table from datalist in aspx.net, vb 2 16
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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