ASP.NET MasterPage How to Access controls within a contentplaceholder

Posted on 2008-06-15
Last Modified: 2013-11-26
I am trying to create property "genMessage" within a masterpage and access the panel control within a contentholder and read/write the text contained within it.  I can access the contentplaceholder,  it shows a control count of 1,  but I do not see how to read/write the text within it (at the moment the text is within a div tag...)






        <asp:contentplaceholder id="MenuContainer" runat="server">

            <a href="~/CustomNavBar.ascx">~/CustomNavBar.ascx</a>



        <asp:contentplaceholder id="mainContentContainer" runat="server">


        <asp:contentplaceholder id="generalMessages" runat="server">


          <asp:Panel ID="msgContent" runat="server"  BackColor="RosyBrown" ForeColor="Aqua" Width="631px">


            This page is still under contruction.  Please check you email for update information or check back soon.









<<<From The Associated MasterPage vb file>>>

    Public Property genMessage() As String


            Dim cph As ContentPlaceHolder = New ContentPlaceHolder

            cph = Me.FindControl("generalMessages")

            If Not cph Is Nothing Then

                Dim lctrl As LiteralControl = New LiteralControl

                lctrl = CType(cph.Controls.Item(0), LiteralControl)

                Return pnl.ToString


                Return String.Empty

            End If

        End Get

        Set(ByVal value As String)

            Dim pnl As Panel = New Panel

            pnl = Me.FindControl("msgContent")

            If Not pnl Is Nothing Then

                Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "GENERALMESSAGE", value)

                'Page.RegisterClientScriptBlock("SETGENERALMESSAGE", value)

            End If

        End Set

    End Property

Open in new window

Question by:hbash
  • 2
LVL 18

Accepted Solution

Ajay Sharma earned 250 total points
ID: 21791083
I think line no. 37 should be corrected.

instead of
Return pnl.ToString

it should be
Return lctrl.ToString


Secondly Panel control does not have a Text property, so if you want to get set the text use label inside panel
LVL 13

Assisted Solution

sm394 earned 250 total points
ID: 21791748
you can access controls in contentplaceholder  by using  controls client ID property
<%= btnSearch.ClientID %> In your case  msgContent.ClientID
also this is good article about Master pages and content pages
read section "FindControl, JavaScript, and Naming Containers" for more info
LVL 13

Expert Comment

ID: 21791802

Author Closing Comment

ID: 31467453
I appreciate the pointers.  I only wish that one of my experts had included some trival complete example.  But,  definitely headed me in the right direction.

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now