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

In most of the projects we need get and set Master page DIV tag from our code behind. We knew that we can do it by using Asp.Net FindControl() method. But after that we need to cast to get the InnerText or InnerHtml of the DIV tag. For Asp.Net controls we can directly cast those as control type. But when you need to get or set values of a DIV tag you need to cast it to HtmlGenericControl. Lets check how we can “Get Set server side DIV of a Master page from Content page Code behind using Asp.Net C# VB.Net“.

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

Related Page:
How to get set server side DIV of a Content page from Master 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

Modify Master Page Title Block Like Below:

            <div runat="server" id='divTitle' class="title">
                <h1>
                    My ASP.NET Application
                </h1>
            </div>

Design your Content Page Like Below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="Button1" runat="server" Text="Get Master Page DIV Content" onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Set Master Page DIV Content" onclick="Button2_Click" />
    <br />
    <hr />
    <b>Master Page Title Div Content:</b><asp:Label ID="lblMasterPageTitle" runat="server" Text=""></asp:Label>
</asp:Content>

Code Example to Get & Set the DIV tag from Codebehind:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //lblMasterPageTitle.Text = ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerText;
        lblMasterPageTitle.Text = ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerHtml;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerText = "Test Application";
        ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerHtml = "<h1>Test Application</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;
        lblMasterPageTitle.Text = DirectCast(Master.FindControl("divTitle"), System.Web.UI.HtmlControls.HtmlGenericControl).InnerHtml
    End Sub
    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        '((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divTitle")).InnerText = "Test Application";
        DirectCast(Master.FindControl("divTitle"), System.Web.UI.HtmlControls.HtmlGenericControl).InnerHtml = "<h1>Test Application</h1>"
    End Sub

If any issue, Please share with us.

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