我的菜单在母版页,如何更改菜单点击后的效果
网友问:
“ 我的菜单在母版页中,用li做的,选中的菜单有一个Selected类,设置选中后的背景。
问题出来了,我其他所有页面都是基于这个母版页,点击菜单转到其他页面,怎么修改这个被点击的菜单的背景呢?
只有选中的菜单才有Selected类。
那就是怎么去除之前选择的菜单Selected类, 而把被点击的菜单加上Selected类??????????? ”
Insus.NET模拟演示:
所有参数在子页面设置与传给MasterPage。本例子中,在masterpage拉了四个LinkButton. 在每个子页怎样去找到Masterpage相对应的LinkButton呢?用Findcontrol(),用foreach去循环所有LinkButton?
Insus.NET惯用的方法,就是写了一个接口Interface。因为所有子网页,只管设置参数与指定目标。而怎样找到目录,不是子网页的事情。
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISetValue
/// </summary>
namespace Insus.NET
{
public interface ISetValue
{
void SetValue(string colorName,string pageName);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISetValue
/// </summary>
namespace Insus.NET
{
public interface ISetValue
{
void SetValue(string colorName,string pageName);
}
}
MasterPage实现这个接口中:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class MySite : System.Web.UI.MasterPage,ISetValue
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetValue(string colorName, string pageName)
{
foreach (Control ctl in menu.Controls)
{
if (ctl is LinkButton)
{
LinkButton lb = (LinkButton)ctl;
if (lb.PostBackUrl.EndsWith (pageName))
lb.BackColor= System.Drawing.Color.FromName(colorName);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class MySite : System.Web.UI.MasterPage,ISetValue
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetValue(string colorName, string pageName)
{
foreach (Control ctl in menu.Controls)
{
if (ctl is LinkButton)
{
LinkButton lb = (LinkButton)ctl;
if (lb.PostBackUrl.EndsWith (pageName))
lb.BackColor= System.Drawing.Color.FromName(colorName);
}
}
}
}
Masterpage.aspx:
View Code
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MySite.master.cs" Inherits="MySite" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Masterpage</legend>
<div id="menu" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" BorderWidth="1" Text="PageA" PostBackUrl="~/Page1.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" BorderWidth="1" Text="PageB" PostBackUrl="~/Page2.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" BorderWidth="1" Text="PageC" PostBackUrl="~/Page3.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" BorderWidth="1" Text="PageD" PostBackUrl="~/Page4.aspx"></asp:LinkButton>
</div>
</fieldset>
<fieldset>
<legend>Content</legend>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</fieldset>
</div>
</form>
</body>
</html>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Masterpage</legend>
<div id="menu" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" BorderWidth="1" Text="PageA" PostBackUrl="~/Page1.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" BorderWidth="1" Text="PageB" PostBackUrl="~/Page2.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" BorderWidth="1" Text="PageC" PostBackUrl="~/Page3.aspx"></asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" BorderWidth="1" Text="PageD" PostBackUrl="~/Page4.aspx"></asp:LinkButton>
</div>
</fieldset>
<fieldset>
<legend>Content</legend>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</fieldset>
</div>
</form>
</body>
</html>
所有子页面的cs代码:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
void Data_Binding()
{
string path = Request.RawUrl;
string pageName = path.Substring(path.LastIndexOf('/'));
((ISetValue)this.Master).SetValue("#ff0000", pageName); //注意啦,每个子页面参数在这里设置!!!
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
void Data_Binding()
{
string path = Request.RawUrl;
string pageName = path.Substring(path.LastIndexOf('/'));
((ISetValue)this.Master).SetValue("#ff0000", pageName); //注意啦,每个子页面参数在这里设置!!!
}
}