How to implement a custom panel with INamingContainer?

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply