How to get set server side DIV of a Content page from Master page Code behind in Asp.Net C# VB.Net

In my previous post I have described how we can Get & Set value of a Master page DIV control. It’s also equally important to know “how we can Get & Set value of a Content page or Child page from Master Page”. Here in this post I will try to make you understand the way to resolution. For server side controls we can easily cast it by using the control type but for DIV we have to cast it by HtmlGenericControl. Hope now you can understand. Let’s check the issue with an example.

Output/Outcome:
How to get set server side DIV of a Content page from Master page Code behind in Asp.Net C# VB.Net

Related Post:
How to get set server side DIV of a Master page from Content page Code behind in Asp.Net C# VB.Net
Javascript FindControl to get or read Content Page Controls Content data from Asp.Net Master Page C# VB.Net
Javascript FindControl to get or read Master Page Controls Content data from Asp.Net Content Page C# VB.Net
How to Read Access Get Set Child Page Controls from Master Page using Asp.Net C# VB.net
How to Read Access Get Set Master Page Controls from Child Page using Asp.Net C# VB.net

Add below HTML code in the Master Page like Below:

            <asp:Button ID="Button1" runat="server" Text="Get Child Page Div Content" onclick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Set Child Page Div Content" onclick="Button2_Click" /><hr />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

Add a DIV tag in Child Page:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id='div_ChildPage' runat="server"><h1>I am in Child Page !!</h1></div>
</asp:Content>

Now write the code under Master Page:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //lblMasterPageTitle.Text = ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerText;
        Label1.Text = ((System.Web.UI.HtmlControls.HtmlGenericControl)MainContent.FindControl("div_ChildPage")).InnerHtml;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //((System.Web.UI.HtmlControls.HtmlGenericControl)MainContent.FindControl("div_ChildPage")).InnerText = "<h1>Changed from Master Page !!</h1>";
        ((System.Web.UI.HtmlControls.HtmlGenericControl)MainContent.FindControl("div_ChildPage")).InnerHtml = "<h1>Changed from Master Page !!</h1>";
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        'lblMasterPageTitle.Text = ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerText;
        Label1.Text = DirectCast(MainContent.FindControl("div_ChildPage"), System.Web.UI.HtmlControls.HtmlGenericControl).InnerHtml
    End Sub
    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        '((System.Web.UI.HtmlControls.HtmlGenericControl)MainContent.FindControl("div_ChildPage")).InnerText = "<h1>Changed from Master Page !!</h1>";
        DirectCast(MainContent.FindControl("div_ChildPage"), System.Web.UI.HtmlControls.HtmlGenericControl).InnerHtml = "<h1>Changed from Master Page !!</h1>"
    End Sub

Now run the code. Hope you can understand. Please let us know if any issue.

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
     
 

*