Asked by:
How to access contentplace control in class file

Question
-
User-805182976 posted
How to access contentplace(child page) control in class file >
Sunday, March 1, 2020 12:14 PM
All replies
-
User409696431 posted
Clarifying: there is no ContentPlace control. There are ContentPlaceHolder controls in the master page, and corresponding Content controls in pages using that master page.
A Content control is not added to the control hierarchy at runtime. Instead, the contents within the Content control are directly merged into the corresponding ContentPlaceHolder control.
If you are looking for controls in a Content area of a page, you actually look for them inside the corresponding ContentPlaceHolder control.
For example, say I have a master page with a ContentPlaceHolder with an id "ContentPlaceHolder1", and in the page using that Master page, I put a Label in that content area with an id "Label1".
This would work to find that Label:
protected void Page_Load(object sender, EventArgs e) { Label mylabel = (Label)Page.Master.FindControl("ContentPlaceHolder1").FindControl("Label1"); mylabel.Text = "I've been found."; }
If you will be dealing with multiple controls, you can shorten things a bit by finding the ContentPlaceHolder1 once and re-using it. Assume I have two labels in the content area, Label1 and Label2. This would work to put text in both Labels:
protected void Page_Load(object sender, EventArgs e) { ContentPlaceHolder placeholder1 = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1"); Label mylabel = (Label)placeholder1.FindControl("Label1"); mylabel.Text = "I've been found."; Label mylabel2 = (Label)placeholder1.FindControl("Label2"); mylabel2.Text = "I've been found also."; }
Now, getting to your full question, what exactly do you want your class file to do? Why does it need to access controls in a content area? Typically you'd access controls in the .cs file for the page, and pass those controls as a parmeter to your class file. For example, if I wanted to add a third label with id Label3, and fill it's value from a class file, this would work. In this case I'm passing both the value to fill and the label to fill to the class function.
.cs file in App_Code
using System.Web.UI.WebControls; namespace ClassLibrary1 { public class FillLabel { public static bool Populate(string value, Label mylabel) { mylabel.Text = value; return (true); } } }
Modified code behind page:
using System; using System.Web.UI; using System.Web.UI.WebControls; using ClassLibrary1; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ContentPlaceHolder placeholder1 = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1"); Label mylabel = (Label)placeholder1.FindControl("Label1"); mylabel.Text = "I've been found."; Label mylabel2 = (Label)placeholder1.FindControl("Label2"); mylabel2.Text = "I've been found also."; Label mylabel3 = (Label)placeholder1.FindControl("Label3"); FillLabel.Populate("The class file found this label.", mylabel3); } }
Monday, March 2, 2020 5:44 AM -
User-1330468790 posted
Hi, Nipesh,
You can find the description of the 'Content' control from MSDN ,
"A Content control is used only with a master page that defines a corresponding ContentPlaceHolder control. A Content control is not added to the control hierarchy at runtime. Instead, the contents within the Content control are directly merged into the corresponding ContentPlaceHolder control."
That means if you want to access the controls in the 'content' control, you will have to get a reference to the 'ContentPlaceHolder' control in the master page.
You can not only access the controls but also add the controls in 'ContentPlaceHolder' control.
More details, please see below code:
Master page:
<div class="container body-content"> <asp:ContentPlaceHolder ID="MainContent" runat="server"> </asp:ContentPlaceHolder> <hr /> <footer> <p>© <%: DateTime.Now.Year %> - My ASP.NET Application</p> </footer> </div>
.asp Page:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <asp:Label ID="TestLabel1" runat="server" Text="TestLabel1"></asp:Label> <br /> <asp:Button ID="TestButton1" runat="server" Text="TestButton1" /> <br /> <asp:Literal ID="TestLiteral1" runat="server" Text="TestLiteral1"></asp:Literal> <hr /> <asp:Label ID="DisplayLabel1" runat="server" Text=""></asp:Label> <hr /> <h4>Add Button below:</h4> </asp:Content>
Code behind:
protected void Page_Load(object sender, EventArgs e) { ContentPlaceHolder mainContent =(ContentPlaceHolder) this.Master.FindControl("MainContent"); DisplayLabel1.Text += "Try to find controls from the Content Place holder in the Master Page: <br/>"; if (mainContent.FindControl("TestLabel1") != null) { DisplayLabel1.Text += "Find TestLabel1: Type: " + mainContent.FindControl("TestLabel1").GetType().ToString() + "<br/>"; } if (mainContent.FindControl("TestButton1") != null) { DisplayLabel1.Text += "Find TestButton1: Type: " + mainContent.FindControl("TestButton1").GetType().ToString() + "<br/>"; } if (mainContent.FindControl("TestLiteral1") != null) { DisplayLabel1.Text += "Find TestLiteral1: Type: " + mainContent.FindControl("TestLiteral1").GetType().ToString() + "<br/>"; } Button btn2 = new Button(); btn2.Text = "TestButton2"; btn2.Visible = true; btn2.ID = "TestButton2"; //Add btn mainContent.Controls.Add(btn2); }
Demo:
Hope this can help you.
Best regards,
Sean
Monday, March 2, 2020 7:27 AM