views:

209

answers:

3

I can't find a good article that solves my following issue, so if anyone has the answer or just a link to the answer I would be happy.

Let's say I have created a UserControl called MyUserControl.ascx

I reg it in ascx with prefix uc

<uc:MyUserControl runat="server" id="uc_test" SomeProperty="true">
<InnerContent>
  ...
  Controls added in here....
  <asp:Button runat="server" id="btn_test" Text="Test">
  ...
<InnerContent>
</uc:MyUserControl>

I know how to create a usercontrol and how to add properties and events to it.

BUT how do I make the "InnerContent" field in my usercontrol?

I have no clue so please be a little specific :)

Cheers

+2  A: 

You usually don't do that with user controls (.ascx). It's completely possible though. This is mostly done in custom controls you build using code files. To accomplish it, you declare your control class like:

[ParseChildren(true), PersistChildren(false)]
public class MyControl : Control, INamingContainer {

   [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(InnerContentTemplate)]
   public ITemplate InnerContent { get; set; }

   void CreateChildControls() { 
       InnerContentTemplate temp = new InnerContentTemplate();
       InnerContent.InstantiateIn(temp);
       Controls.Add(temp);
   }
}

public class InnerContentTemplate : Control, INamingContainer {

}

For .ascx files you could inherit it from UserControl instead.

Templating in ASP.NET is a rather complex thing. It's not really possible to explain everything in an answer. You should look at some samples and documentation.

Mehrdad Afshari
This seems to be exactly what I am trying to accomplish, but could you give an example of the setter function?maybe loading the data into a Panel in the UserControl?
The real napster
Thanks and I get what you are saying, will read up on it. Thanks for your help.
The real napster
This is workign for me but I can't seem to find a way to get the controls I have added in the template back out again.Have any quick hints for me on this one?
The real napster
Tried `Control.FindControl` method?
Mehrdad Afshari
A: 

You want to create a templated control, rather like a Repeater (ie, you have an ItemTemplate region, plus headers and wotnot). Here is an example.

darasd
Thanks for this link, will try it out!
The real napster
A: 

In extension to the comments made to Merhdad's reply: This is working for me but I can't seem to find a way to get the controls I have added in the template back out again. Have any quick hints for me on this one? – The real napster (14 mins ago) Tried Control.FindControl method? – Mehrdad (8 mins ago)

..................

I can find the Template by accessing the panel I added it to.. Something like this:

pnl_content.Controls[index]

but I have no idea how to get the controls from the template..

This is what my code looks like:

<uc:PopupOKCancel ClientInstanceName="pop_createCompany" runat="server" ID="pop_createCompany" OKButtonText="opret" HeaderText="Opret nyt firma">
     <ContentTemplate>
           <uc:CompanyDetails runat="server" id="uc_companyDetails"></uc:CompanyDetails>
     </ContentTemplate>
</uc:PopupOKCancel>

The CompanyDetails UserControl doesn't seem to exists actually.. It seems that only the template exists.. I have tried adding some properties to the Template class so I could set them and get them but that was not possible.. for some reason.

The real napster