Access to content management in C # when using master pages

Good afternoon everyone

I create a page in ASP.NET and use master pages in this process.

I have the name of the content owner "cphBody" on my wizard page, which will contain the body of each page for which this wizard page is the main page.

On an ASP.NET webpage, I have a Content tag (link "cphBody") that also contains some controls (buttons, Infragistics controls, etc.) and I want to access these controls in a CodeBehind file . However, I cannot do this directly (this.myControl ...), since they are nested in the Content tag.

I found a workaround using the FindControl method.

ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody"); ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName"); 

This works great. However, I suspect this is not a very good design. Do you guys know a more elegant way to do this?

Thanks!

Guillaume Gervais.

+6
master-pages findcontrol
source share
5 answers

Rick Strall has a good explanation (and sample code) here - http://www.west-wind.com/Weblog/posts/5127.aspx

+4
source share

I try to avoid FindControl if there is no alternative, and it usually happens in a more neat way.

How about including the path to the main page at the top of your child page

 <%@ MasterType VirtualPath="~/MasterPages/PublicUI.Master" %> 

This will allow you to directly call the code from the code of the main page.

Then, from your main page code, you can force the property to return its control or make a way on the main page to gain control, etc.

 public Label SomethingLabel { get { return lblSomething; } } //or public string SomethingText { get { return lblSomething.Text; } set { lblSomething.Text = value; } } 

Refers to the label on the main page

 <asp:Label ID="lblSomething" runat="server" /> 

Using:

 Master.SomethingLabel.Text = "some text"; //or Master.SomethingText = "some text"; 
+7
source share

That's that. Just write this code on the child page to access the control of the homepage label.

 Label lblMessage = new Label(); lblMessage = (Label)Master.FindControl("lblTest"); lblMessage.Text = DropDownList1.SelectedItem.Text; 
+3
source share

I use this code for acess for files recursively:

  /// <summary> /// Recursively iterate through the controls collection to find the child controls of the given control /// including controls inside child controls. Return all the IDs of controls of the given type /// </summary> /// <param name="control"></param> /// <param name="controlType"></param> /// <returns></returns> public static List<string> GetChildControlsId(Control control, Type controlType) { List<string> FoundControlsIds = new List<string>(); GetChildControlsIdRecursive(FoundControlsIds, control, controlType); // return the result as a generic list of Controls return FoundControlsIds; } public static List<string> GetChildControlsIdRecursive(List<string> foundControlsIds, Control control, Type controlType) { foreach (Control c in control.Controls) { if (controlType == null || controlType.IsAssignableFrom(c.GetType())) { // check if the control is already in the collection String FoundControl = foundControlsIds.Find(delegate(string ctrlId) { return ctrlId == c.ID; }); if (String.IsNullOrEmpty(FoundControl)) { // add this control and all its nested controls foundControlsIds.Add(c.ID); } } if (c.HasControls()) { GetChildControlsIdRecursive(foundControlsIds, c, controlType); } } 
+1
source share

Hi, I just thought that I would share my decision, found that it works to access the "Control", which is located inside the <asp: Panel>, which is located on the "ContentPage", but with the C # code "MasterPage". Hope this helps some.

  • add <asp: Panel> with ID = "PanelWithLabel" and runat = "server" to your ContentPage.

  • inside the panel, add <asp: Label> control with ID = "MyLabel".

  • enter (or copy / paste below) the function into your MasterPage code as follows: (this allows you to access the label control inside the panel, which are located on the ContentPage, from the main page code - behind and change your text to TextBox on the main page :)

     protected void onButton1_click(object sender, EventArgs e) { // find a Panel on Content Page and access its controls (Labels, TextBoxes, etc.) from my master page code behind // System.Web.UI.WebControls.Panel pnl1; pnl1 = (System.Web.UI.WebControls.Panel)MainContent.FindControl("PanelWithLabel"); if (pnl1 != null) { System.Web.UI.WebControls.Label lbl = (System.Web.UI.WebControls.Label)pnl1.FindControl("MyLabel"); lbl.Text = MyMasterPageTextBox.Text; } } 
+1
source share

All Articles