Custom Control ClientID weirdness

O

Olorin

I'm encountering some weird behavior with the ClientID property. Let's
see if I can give you enough info and if anyone has ideas/suggestions.

I'm working in ASP.NET 2.0. I have a Web Project with a master page,
with a single ContentPlaceholder on it.
I have a page that uses that Master template, and is meant to place 2
controls of type Fred in the content placeholder. The .aspx for this
page goes like this:
[register tag prefix cc to grab custom controls from a separate
project]
....
<asp:Content ID="Content1"
ContentPlaceHolderID="contentPlaceholder_Workarea" runat="server">
<cc:Fred runat="Server" ID="fred_1" />
<cc:Fred runat="Server" ID="fred_2"></cc:Fred>
</asp:Content>

The Fred control is actually a custom control I defined as inheriting
from CompositeControl. Fred overrides TagKey to return
HtmlTextWriterTag.Div. It also overrides CreateChild Control to do a
few things, among which, it adds a System.Web.UI.WebControls.Panel
named Wilma to its Controls. When I add a Wilma to the controls, I add
an attribute to it like this:
myWilma.Attributes.Add("onclick", "alert('"+this.ClientID+"');");

It all works quite nicely, and the Wilma's have the Freds' mangled Ids
(e.g.: onclick="alert('ctl00_contentPlaceholder_Workarea_fred_1')")
available.

However, if I put anything (and I mean anything, even a space
character, a newline character, ...) between the opening and closing
tags of a Fred control, like this:
<cc:Fred runat="Server" ID="fred_2"> </cc:Fred>
Then Wilma's onclick event gets an un-mangled version of Fred's ID
(e.g.: onclick="alert('fred_2');" ).

It seems that I'm missing some crucial step/aspect that makes my
controls' Client IDs go out of sync.
I have checked fred_2's ClientID from the surrounding page Page_Load
method, and it is mangled at that time.
So, the difference is between the times when Page_Load and the
control's CreateChildControls methods run...

Any suggestion ?

Thanks in advance,
Frank
 
M

Masudur

I'm encountering some weird behavior with the ClientID property. Let's
see if I can give you enough info and if anyone has ideas/suggestions.

I'm working in ASP.NET 2.0. I have a Web Project with a master page,
with a single ContentPlaceholder on it.
I have a page that uses that Master template, and is meant to place 2
controls of type Fred in the content placeholder. The .aspx for this
page goes like this:
[register tag prefix cc to grab custom controls from a separate
project]
...
<asp:Content ID="Content1"
ContentPlaceHolderID="contentPlaceholder_Workarea" runat="server">
<cc:Fred runat="Server" ID="fred_1" />
<cc:Fred runat="Server" ID="fred_2"></cc:Fred>
</asp:Content>

The Fred control is actually a custom control I defined as inheriting
from CompositeControl. Fred overrides TagKey to return
HtmlTextWriterTag.Div. It also overrides CreateChild Control to do a
few things, among which, it adds a System.Web.UI.WebControls.Panel
named Wilma to its Controls. When I add a Wilma to the controls, I add
an attribute to it like this:
myWilma.Attributes.Add("onclick", "alert('"+this.ClientID+"');");

It all works quite nicely, and the Wilma's have the Freds' mangled Ids
(e.g.: onclick="alert('ctl00_contentPlaceholder_Workarea_fred_1')")
available.

However, if I put anything (and I mean anything, even a space
character, a newline character, ...) between the opening and closing
tags of a Fred control, like this:
<cc:Fred runat="Server" ID="fred_2"> </cc:Fred>
Then Wilma's onclick event gets an un-mangled version of Fred's ID
(e.g.: onclick="alert('fred_2');" ).

It seems that I'm missing some crucial step/aspect that makes my
controls' Client IDs go out of sync.
I have checked fred_2's ClientID from the surrounding page Page_Load
method, and it is mangled at that time.
So, the difference is between the times when Page_Load and the
control's CreateChildControls methods run...

Any suggestion ?

Thanks in advance,
Frank

Hi,

A asp,net controls client id is generated according to where the
control reside...
i.e. if i add the control in a panel and then add the panel in
usercontrol and at last in page
client id will be usercontrolclientid+panelclientid
+controlclientid.....
if i change my control from one panel to another panel it will change
accordingly...
so its better to generate any script in onrender method of control
life cycle...

Thanks
Masudur
 
O

Olorin

so its better to generate any script in onrender method of control
life cycle...

Thanks
Masudur
Thanks, Masudur, your hint helped me out and I was able to make the
Freds and Wilmas work out as intended.
I was assuming that the Render method in CompositeControl would call
the CreateChildControls method (which I was overriding), but it turns
out that's not the case.

Thanks again,
~O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top