ASP.NET Templated User Controls - Limit child controls allowable within a templated control

ASP.NET Templated User Controls - Limit child controls allowable within a templated control

Post by JohnyStyle » Wed, 31 May 2006 03:00:12


I have a templated user control which looks like this:

-----------------------------------------------------------------
<uc:MyUserControl runat=server>
<TemplateProperty>

</TemplateProperty>
</uc:MyUserControl>
-----------------------------------------------------------------

Currently i can drop any html/web controls/user controls into
TemplateProperty. I then handle these inside of my user control, in
the PreRender, I do run:
_templateProperty.InstantiateIn(aPlaceHolderInsideUserControl);

This all works. Very nice, allows for great content encapsulation.

Now, what I would like to do next is limit what controls are allowable
INSIDE of the TemplateProperty. An example would be only allowing
'asp:Label' to be used, or as in my case, another usercontrol which
I've created.

So idealy, once one types:
-----------------------------------------------------------------
<Template Property>
<[ctrl-space]
-----------------------------------------------------------------

intelisense only allows you to populate the Template Property with
controls that are allowable, i.e. asp:Label, or a another specified
user control, or something of that nature.

ASP.NET actually already does this with some webcontrols. for an
example, look at:
-----------------------------------------------------------------
<asp:DataGrid runat=server>
<Columns>
</Columns>
</asp:DataGrid>
-----------------------------------------------------------------

inside of the Templated Property, Columns, one can only choose from:
<asp:BoundColumn />
<asp:ButtonColumn />
<asp:EditCommandColumn />
<asp:HyperLinkColumn />
<asp:TemplateColumn />

Any other control inserted in <Columns> will cause compile errors:
Error 4 Validation (ASP.Net): Text is not allowed between the opening
and closing tags for element Columns'.

I'd like to have this same functionality with my controls. Any help
would be greatly appreciated!

Thanx Much,
John Pequeno
... just another Computer Engineer
 
 
 

1. Custom control within User control within XML template

2. Issues in ASP.NET 2.0 when dynamically loading user controls or asp.net web controls???

Hello,

I'm new to ASP.NET and I've been looking into the topic of dynamically
loading (typically accomplished with a LoadControl followed by a
MyControl.Controls.Add()) both user controls and asp.net web controls. I
searched for articles on the internet and in the end I must say that I'm
confused.
I read about issues related to:

a.. events not firing
b.. state management
c.. postbacks
but a lot of these articles were old, dealing with v1.1 or even v1.0 of the
framework so I really don't know if any of these issues still exist and what
they are. I know however that Microsoft used this technique of dynamic
controls loading in the old IBuySpy portal starter kit ..... so I maybe it
is not so bad.

Could anyone please briefly recap if any issues still exist in ASP.NET v2.0
when dynamically loading user controls (.ascx) and/or asp.net web
controls???
Thanks.


Bob Rock

3. nUnitASP: HowTo reference ASP.NET UI controls within custom User Control?

4. Composite control delegating databound templated features to child <asp:repeater> control

5. Difference between ASP.NET web controls and ASP.NET mobile control

6. Which ASP.NET Controls Have Child Controls?

7. Add control to asp:PlaceHolder on asp.net page from user control on same page

8. ASP.NET 2.0 User Control that implements a template

9. Add Controls within Treeview Control asp.net v2.0

10. Controls.Add() for a Web User Control doesn't instansiate its child controls

11. Traversing children controls of user control get extra controls

12. this.Controls.Add(Control) Does Not Add Control to child collection (this.Controls.Count == 0)

13. Custom Control VS User Control (NOT ASP.NET)