How to highlight current page or link in asp.net.
Let's see it.
I have a site.master page like below.
-------------------------
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<table><tr>
<td><asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="~/WebForm1.aspx">Page2</asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl="~/WebForm2.aspx">Page3</asp:LinkButton></td>
</tr></table>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
------------------------------------------------------------
Now on site.master.cs file write following code.
public partial class Site1 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changecolor(Page obj)
{
if (obj is WebForm1)
{
LinkButton2.ForeColor = Color.Green;
LinkButton3.ForeColor = Color.Empty;
}
if (obj is WebForm2)
{
LinkButton2.ForeColor = Color.Empty;
LinkButton3.ForeColor = Color.Green;
}
}
}
---------------------------------------------------------
Now on each page load event write just following code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Site1 myMasterPage = (Site1)Page.Master;
myMasterPage.changecolor(this.Page);
}
}
--------
Done..
Let's see it.
I have a site.master page like below.
-------------------------
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<table><tr>
<td><asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="~/WebForm1.aspx">Page2</asp:LinkButton></td>
<td><asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl="~/WebForm2.aspx">Page3</asp:LinkButton></td>
</tr></table>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
------------------------------------------------------------
Now on site.master.cs file write following code.
public partial class Site1 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changecolor(Page obj)
{
if (obj is WebForm1)
{
LinkButton2.ForeColor = Color.Green;
LinkButton3.ForeColor = Color.Empty;
}
if (obj is WebForm2)
{
LinkButton2.ForeColor = Color.Empty;
LinkButton3.ForeColor = Color.Green;
}
}
}
---------------------------------------------------------
Now on each page load event write just following code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Site1 myMasterPage = (Site1)Page.Master;
myMasterPage.changecolor(this.Page);
}
}
--------
Done..
No comments:
Post a Comment