views:

234

answers:

2

I have a Web User Control that holds many others User Controls. I'd like to name those embedded controls based on the ID of the parent user control.

I'm trying this code inside the main User Control:

<myLibrary:myChildUserControl1 ID="<%=ID%>" runat="server" />

or

<myLibrary:myChildUserControl2 ID="<%=ID%>_OkButton" runat="server" />

In both cases I get a compiler error.

Note that I have no problem assigning the control ID to regular HTML elements. The following code works well:

<div ID="<%=ID%>" >

What I am missing?

+1  A: 

In ASP.NET server control's ID is generated based on its Naming container. If you want you child controls to have ID's generated based on its parent, your parent control should implement the INamingContainer interface

Branislav Abadjimarinov
A: 

Hey,

You can't dynamically generate the server-side ID's like you are trying above. Only if you were programmatically creating the user controls would that work, but that poses a problem too. Client ID's can be generated though; if the is within the user control, you should be able to do that, or add a property in your user control:

public string DIVID { get { return this.ClientID + "_div"; } }

And then write out that within the DIV.

Brian