How to Read Access Get Set Master Page Controls from Child 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 Master Page control from Content Page. That’s why in this Asp.Net tutorial i will explain how you can access or read Master page controls from Child Page or Content Page. Have a look at the below output:

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

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

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

         <asp:Label ID="lblMaster" runat="server" Text="Master Label"></asp:Label>
         <asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>

To do the example Add some content in Child Page:

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

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

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label lblMaster = (Label)Master.FindControl("lblMaster");
        TextBox txtMaster = (TextBox)Master.FindControl("txtMaster");

        if (lblMaster != null)
            lblChild.Text = lblMaster.Text;
        if (txtMaster != null)
            txtChild.Text = txtMaster.Text;
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim lblMaster As Label = DirectCast(Master.FindControl("lblMaster"), Label)
        Dim txtMaster As TextBox = DirectCast(Master.FindControl("txtMaster"), TextBox)

        If lblMaster IsNot Nothing Then
            lblChild.Text = lblMaster.Text
        End If
        If txtMaster IsNot Nothing Then
            txtChild.Text = txtMaster.Text
        End If
    End Sub

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

    protected void Button2_Click(object sender, EventArgs e)
    {
        Label lblMaster = (Label)Master.FindControl("lblMaster");
        TextBox txtMaster = (TextBox)Master.FindControl("txtMaster");

        if (lblMaster != null)
            lblMaster.Text = lblChild.Text;
        if (txtMaster != null)
            txtMaster.Text = txtChild.Text;
    }

VB.Net Code:

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        Dim lblMaster As Label = DirectCast(Master.FindControl("lblMaster"), Label)
        Dim txtMaster As TextBox = DirectCast(Master.FindControl("txtMaster"), TextBox)

        If lblMaster IsNot Nothing Then
            lblMaster.Text = lblChild.Text
        End If
        If txtMaster IsNot Nothing Then
            txtMaster.Text = txtChild.Text
        End If
    End Sub

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

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*