user controls – ASP.NET UserControls – how do I make a content field accessible from aspx code? – Education Career Blog

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

,

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.

,

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.

,

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.Controlsindex

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.

Leave a Comment