Accessing Control of Master Page From the Content Page in ASP.NET Web Application

Sometimes we need to access the control of the master page from the content page. We can do this very easily. Just use the following procedure and see the magic for accessing the controls of the master page.

So, let us start by creating a new web site.

Open the Visual Studio editor.

Step 1

Go to the File menu and select new website as in the following.

Image1.jpg

Step 2

Now give a name to your newly created website (say AccessMasterControls) and specify a location to save it and finally press the Ok button.

Image2.jpg

Step 3

  • Now open Solution Explorer and select "Add new item" so that you can add a master page to your application.

    Image3.jpg

  • Now select "Master Page" and give a suitable name to your master page. In this case I have named it Master1.master.

    Image4.jpg
  • Your Master1.master page will look like this:

    Image5.jpg

Step 4

Now take two labels and two textboxes just above the ContentPlaceHolder in the Master1.master page. Set their ids as Label1, Label2, TextBox1, and TextBox2 respectively.

  • The code will be as in the following:
    1. <body>  
    2.     <form id="form1" runat="server">  
    3.     <div style="background-color:Blue; color:White; font-size:large">  
    4.     Master Page  
    5.     </div>  
    6.     <div>  
    7.         <table class="style1">  
    8.             <tr>  
    9.                 <td class="style2">  
    10.                      </td>  
    11.                 <td>  
    12.                      </td>  
    13.             </tr>  
    14.             <tr>  
    15.                 <td class="style2">  
    16.                     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
    17.                 </td>  
    18.                 <td>  
    19.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
    20.                 </td>  
    21.             </tr>  
    22.             <tr>  
    23.                 <td class="style2">  
    24.                      </td>  
    25.                 <td>  
    26.                      </td>  
    27.             </tr>  
    28.             <tr>  
    29.                 <td class="style2">  
    30.                     <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>  
    31.                 </td>  
    32.                 <td>  
    33.                     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
    34.                 </td>  
    35.             </tr>  
    36.             <tr>  
    37.                 <td class="style2">  
    38.                      </td>  
    39.                 <td>  
    40.                      </td>  
    41.             </tr>  
    42.         </table>  
    43.    </div>  
    44.      <div>  
    45.         <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">  
    46.         </asp:ContentPlaceHolder>  
    47.     </div>  
    48.     </form>  
    49. </body>
  • The design view will look like this:

    Image6.jpg

Step 5

  • Now add a content page to your application.

    For this go to the Solution Explorer and "Add new item" then select "webform" and give a name to the web page. In this case I have named it "myPage.aspx".

    Check the checkbox "select master page" and click the "Add" button.

    Image7.jpg

    Image8.jpg 

  • Now you will be prompted to select a master page for your content page from all the master pages available in your application. In this case we have only one master page, namely Master1.master. So, select it and click the Ok button.

    Image9.jpg

  • Now see your myPage.aspx page will look like this:

    Image10.jpg
     

  • Now in the myPage.aspx page write some text and take a button.

  • Now go to the myPage.aspx.cs page and write some code on the click event of the button.


    Image11.jpg


Here I will access the labels and textboxes of the master page on the click event of the button.

Here I am finding the label and textbox controls of the master page and changing the text of these controls. The code is as in the following.

Image12.jpg

Step 6

Now run your myPage.aspx page in the browser and see the result.

Image13.jpg

Just click the show button and see the magic.

Image14.jpg

Conclusion

Here we have seen how to access the controls of the master page from the content page.

In this way we can access any control of the master page from the content page.