Set ASP: ContentPlaceHolder Content Programatically

What is the easiest way to programmatically set the contents of <asp:ContentPlaceHolder> ? I think you need to make a call to Master.FindControl ?

+1
source share
3 answers

If your page inherits from MasterPage, then you should have an asp: Content control on your page with some id, for example:

 <asp:Content runat="server" ID="myContent" ContentPlaceHolderID="masterContent"> </asp:Content> 

You should be able to reference this in your code and add whatever you want.

 public void Page_Load( object sender, EventArgs e ) { HtmlContainerControl div = new HtmlGenericControl( "DIV" ); div.innerHTML = "....whatever..."; myContent.Controls.Clear(); myContent.Controls.Add(div); } 
+4
source

If you add controls to a blank page, you do Page.Controls.Add () .... no?

0
source

I use my own extension method, which searches for a control (e.g. a placeholder, for example) recursively to find the control you are looking for by identifier and return it. After that, you can fill in the returned control. Call this in a foreach loop, iterating over your list of controls to populate.

 public static class ControlExtensions { /// <summary> /// recursive control search (extension method) /// </summary> public static Control FindControl(this Control control, string Id, ref Control found) { if (control.ID == Id) { found = control; } else { if (control.FindControl(Id) != null) { found = control.FindControl(Id); return found; } else { foreach (Control c in control.Controls) { if (found == null) c.FindControl(Id, ref found); else break; } } } return found; } } 
0
source

Source: https://habr.com/ru/post/915140/


All Articles