ASP.NET Master Page Controls and Accessing From a Content Page

By TSN ...

The sample below describes how we can access the masterPage controls from the content page. Suppose we have a TextBox in the master page and we need to set the text to in the click event of Button which is in Content page.

Here we can simply find the control of textbox which is in the master page and assign text to it.

Here is the solution

Content Page Default.aspx

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
    <asp:Button ID="btnChildControl" runat="server" Text="Set the MasterPage Control"
             
onclick="btnChildControl_Click" />
     
         <asp:TextBox ID="txtContentControl" runat="server"></asp:TextBox>
    </div>
</asp:Content>

handling button click event in Default.aspx

protected void btnChildControl_Click(object sender, EventArgs e)
    {
         //txtMasterControl is the TextBox Control in the MasterPage.
       TextBox txtName = this.Master.FindControl("txtMasterControl") as TextBox;
        txtName.Text = "Called from content page ";

    }

ASP.NET Master Page Controls and Accessing From a Content Page  (1549 Views)