views:

71

answers:

2

I would like to have a user control where it's rendered HTML is logically divided up into sections. I would like an aspx page to dynamically load this user control that take each section and places them at specific points in the aspx page. Is this possible?

+1  A: 

Assuming that the sections that you want to render are not contiguous, no. Your options are to separate your control into separate controls for each section, or to create a class with methods/properties that return the code or control list for each section separately.

David Lively
+1  A: 

Sure it's possible. Make your usercontrol like this:

<asp:PlaceHolder runat="server" id="section1">
content
</asp:Placeholder>
<asp:PlaceHolder runat="server" id="section2">
content
</asp:Placeholder>
<asp:PlaceHolder runat="server" id="section3">
content
</asp:Placeholder>

and the code behind add 3 properties like this:

public Control Section1
{
   get{return section1;}
}

public Control Section2
{
   get{return section2;}
}

public Control Section3
{
   get{return section3;}
}

then, in your aspx you would have 3 placeholders representing the 3 places where you want the sections of the user control to go. Code in the aspx page_load method would look like this:

MyUsercontrol c = LoadControl("MyUsercontrol.ascx") as MyUsercontrol;
placeholder1.Controls.Add(c.Section1);
placeholder2.Controls.Add(c.Section2);
placeholder3.Controls.Add(c.Section3);
Matthew
+1 for example code.
David Lively