Access Master Page Control In Child Page

1. Create a new Web Application
2. Right Click On Your Application and Choose Add Asp.Net Folder and Select App_code Folder
3. Right Click On App_Code Folder and Select Add New Item and Choose Class
4. Give the Name of Class "Sambhav"
5. Copy and Paste the following codding in your new created class(Sambhav.cs)
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
 /// <summary> /// Summary description for Sambhav /// </summary> 
public class Sambhav 
 public Sambhav() 
 { 
// // TODO: Add constructor logic here // 
 } 
 } 
public class MyStyle 
public static string MasterStyle() 
string strStyle = "display: inline; padding:7px 20px 7px 20px; color:#fff; background: #209abc; background: -moz-linear-gradient(top, #209abc 0%, #006b8b 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#209abc), color-stop(100%,#006b8b)); background: -webkit-linear-gradient(top, #209abc 0%,#006b8b 100%); background: -o-linear-gradient(top, #209abc 0%,#006b8b 100%); background: -ms-linear-gradient(top, #209abc 0%,#006b8b 100%); background: linear-gradient(to bottom, #209abc 0%,#006b8b 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#209abc', endColorstr='#006b8b',GradientType=0 ); text-decoration:none;"; return strStyle; 
}
 
6. Create a Master Page In Your Application
7.. Type the name of Master Page "Sambhav.master"
8. Paste the following codding in your Master Pag
 
<!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"> 
 <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr style="background: #209abc; background: -moz-linear-gradient(top, #209abc 0%, #006b8b 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#209abc), color-stop(100%,#006b8b)); background: -webkit-linear-gradient(top, #209abc 0%,#006b8b 100%); background: -o-linear-gradient(top, #209abc 0%,#006b8b 100%); background: -ms-linear-gradient(top, #209abc 0%,#006b8b 100%); background: linear-gradient(to bottom, #209abc 0%,#006b8b 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#209abc', endColorstr='#006b8b',GradientType=0 ); color:White;"> <td colspan="2" align="center"> Heading </td> </tr> <tr> <td style="width:20%"> <table> <tr> <td style="height:40px;"> <asp:LinkButton ID="lnkPage1" runat="server" Text="MasterPageLinkButton1" PostBackUrl="~/Page1.aspx" CausesValidation="false"></asp:LinkButton> </td> </tr> <tr> <td style="height:40px;"> <asp:LinkButton ID="lnkPage2" runat="server" Text="MasterPageLinkButton2" PostBackUrl="~/Page2.aspx" CausesValidation="false"></asp:LinkButton> </td> </tr> </table> </td> <td style="width:80%"> <table> <tr> <td> <asp:ContentPlaceHolder ID="cntBody" runat="server"> </asp:ContentPlaceHolder> </td> </tr> </table> </td> </tr> </table> 
 </form> 
</body> 
</html>
 
9. Add New Page In Your Application Page1.aspx and Check On Select Master Page Checkbox and Choose Sambhav.master
10. Add Following Codding In Your Page1.aspx
 
<table>
<tr>
<td>
Page 1<br />
Now On This Page Your Menu Bar Link Button Has Been Changed, Master Page Control Accessing By Child Page
</td>
</tr>
</table>

11. Add Following Codding On Page Load in Page1.aspx.cs File
 
protected void Page_Load(object sender, EventArgs e) 
 { 
 //Following Line :- Master Page Control(lnkPage1) Accessing By Child Page 
 LinkButton lnkButton = (LinkButton)Master.FindControl("lnkPage1"); 
 lnkButton.Attributes.Add("style", MyStyle.MasterStyle()); 
 }
 
12. Add New Page In Your Application Page2.aspx and Check On Select Master Page Checkbox and Choose Sambhav.master
13. Add Following Codding In Your Page2.aspx
<table>
<tr>
<td>
Page 2<br />
Now On This Page Your Menu Bar Link Button Has Been Changed, Master Page Control Accessing By Child Page
</td>
</tr>
</table>
 
14. Add Following Codding On Page Load in Page2.aspx.cs File
 
protected void Page_Load(object sender, EventArgs e) 
 { 
 //Following Line :- Master Page Control(lnkPage2) Accessing By Child Page 
 LinkButton lnkButton = (LinkButton)Master.FindControl("lnkPage2"); 
 lnkButton.Attributes.Add("style", MyStyle.MasterStyle()); 
 }
 

15. Press F5 in Your Application and After Executing Application In Your Local Host Please Click On Left Menubar, Now You will find that Menubar's Linkbutton Style being change, Because In this application On Page Load Master Page's Control is Accessing By Child Page.

Ebook Download
View all
Learn
View all