views:

35

answers:

1

I'm trying to implement a custom panel control that would act as a naming container. So far here is so what I've done.

First this is my custom control, MyPanel...

[ToolboxData("<{0}:MyPanel runat=server></{0}:MyPanel>")]
public class MyPanel: Panel, INamingContainer
{
}

And I try using it like so:

<cc1:MyPanel ID="A" runat="server">
    <asp:HyperLink ID="TestHyperLink" runat="server" />
</cc1:MyPanel>
<cc1:MyPanel ID="B" runat="server">
    <asp:HyperLink ID="TestHyperLink" runat="server" />
</cc1:MyPanel>

Obviously it doesn't work, that would have been too easy. ASP.net still complains about there being 2 DocumentHyperLink:

The ID 'DocumentHyperLink' is already used by another control.

How am I supposed to approach this problem?

Thank you.

+1  A: 

It sounds like you might benefit from a templated control design, instead of from a Panel design. Here are some resources to get started with templated controls:

The appeal of templated designs is that repeating a server control ID from one template in another is permitted.

kbrimington
Thanks, I had actually seen this before. However, I really wanted to try a specialized Panel, not a templated control. Is there really no way of repeating an ID without using templates?
md1337
@md1337: I'm not sure. The Panel design allows server controls within the panel to be associated with member variables of the page's class. Templates do not. I can't think of an out-of-the-box control with the behavior you describe. Panels, Wizards, MultiViews all are non-templated and all exhibit the behavior you are encountering. I'm a little curious why you want to avoid templates?
kbrimington
@kbrimington: I guess I just wanted to avoid the clutter of templates, when all I need is a simple container such as Panel, with the added functionality of providing unique ids to its child controls. However, It seems like the only solution is a custom templated control like you mentioned. There is actually a good solution at http://stackoverflow.com/questions/1912283/asp-net-user-control-inner-content
md1337