How to Read Access Get Set Child Page Controls from Master Page using Asp.Net C# VB.net

In many forums i found that Asp.Net C# VB.Net developers ask expert how to get or read or set Child Page control from Master Page. That’s why in this Asp.Net tutorial i will explain how you can access or read Child page controls from Master Page. Have a look at the below output:

How to Read Access Get Set Child Page Controls from Master Page using Asp.Net Csharp VB.net

Related Article:
Javascript FindControl to get or read Content Page Controls Content data from Asp.Net Master Page C# VB.Net

First Add some content in Child Page to read from Master Page:

<asp:Label ID="lblChild" runat="server" Text="Child Label"></asp:Label>
<asp:TextBox ID="txtChild" runat="server"></asp:TextBox>

To do the example Add some content in Master Page:

            <asp:Button ID="Button1" runat="server" Text="Get Child Page Content" 
                onclick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Set Child Page Content" 
                onclick="Button2_Click" />       
            <br />
            <asp:Label ID="lblMaster" runat="server" Text="Master Label"></asp:Label>
            <asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>
            <br />
            <hr />

Now to Get Child Page Content Write below Code in Master Page:
C# Code

    protected void Button1_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.FindControl("MainContent");
        if (ContentPlaceHolder1 != null)
        {
            Label lblChild = (Label)ContentPlaceHolder1.FindControl("lblChild");
            TextBox txtChild = (TextBox)ContentPlaceHolder1.FindControl("txtChild");
            if (lblChild != null)
                lblMaster.Text = lblChild.Text;
            if (txtChild != null)
                txtMaster.Text = txtChild.Text;
        }
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim ContentPlaceHolder1 As ContentPlaceHolder = DirectCast(Me.FindControl("MainContent"), ContentPlaceHolder)
        If ContentPlaceHolder1 IsNot Nothing Then
            Dim lblChild As Label = DirectCast(ContentPlaceHolder1.FindControl("lblChild"), Label)
            Dim txtChild As TextBox = DirectCast(ContentPlaceHolder1.FindControl("txtChild"), TextBox)
            If lblChild IsNot Nothing Then
                lblMaster.Text = lblChild.Text
            End If
            If txtChild IsNot Nothing Then
                txtMaster.Text = txtChild.Text
            End If
        End If
    End Sub

Now to Set Child Page Data Write below Code in Master Page:
C# Code:

    protected void Button2_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.FindControl("MainContent");
        if (ContentPlaceHolder1 != null)
        {
            Label lblChild = (Label)ContentPlaceHolder1.FindControl("lblChild");
            TextBox txtChild = (TextBox)ContentPlaceHolder1.FindControl("txtChild");
            if (lblChild != null)
                lblChild.Text = lblMaster.Text;
            if (txtChild != null)
                txtChild.Text = txtMaster.Text;
        }
    }

VB.Net Code:

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        Dim ContentPlaceHolder1 As ContentPlaceHolder = DirectCast(Me.FindControl("MainContent"), ContentPlaceHolder)
        If ContentPlaceHolder1 IsNot Nothing Then
            Dim lblChild As Label = DirectCast(ContentPlaceHolder1.FindControl("lblChild"), Label)
            Dim txtChild As TextBox = DirectCast(ContentPlaceHolder1.FindControl("txtChild"), TextBox)
            If lblChild IsNot Nothing Then
                lblChild.Text = lblMaster.Text
            End If
            If txtChild IsNot Nothing Then
                txtChild.Text = txtMaster.Text
            End If
        End If
    End Sub

Hope now you can Access Child Page Contents as well as can set also data from Master Page.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code