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:
     

    <body>

        <form id="form1" runat="server">

       

       

        <div style="background-color:Blue; color:White; font-size:large">

        Master Page

        </div>

        <div>

       

            <table class="style1">

                <tr>

                    <td class="style2">

                        &nbsp;</td>

                    <td>

                        &nbsp;</td>

                </tr>

                <tr>

                    <td class="style2">

                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="style2">

                        &nbsp;</td>

                    <td>

                        &nbsp;</td>

                </tr>

                <tr>

                    <td class="style2">

                        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td class="style2">

                        &nbsp;</td>

                    <td>

                        &nbsp;</td>

                </tr>

            </table>
          

       </div>

       

         <div>

            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

           

            </asp:ContentPlaceHolder>

        </div>

        </form>

    </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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all