472,111 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

EnsureChildControls causes UniqueID to be incorrect inside UserControl

I have a custom control that is thrown onto a UserControl that is
thrown onto a WebForm. Basically, I've got a scenario where if my
UserControl sets an attribute on the custom control in the ascx,
EnsureChildControls gets called, creating the control before the
UserControl is even added to the webform. The problem with this is
that the UniqueID of the custom control will be false, since OnInit
has yet to be called on its parent, somehow screwing up the creation
of its UniqueID. This results in all events from that control
failing. Here's a little pseudocode to help explain:

public string CustomProp1
// Set something here.

<myTag:MyCustomControl id="myCustomID" CustomProp1="something"
runat="server" />

OnLoad(EventArgs e)
MyUserControl control = LoadControl("MyUserControl.ascx");
// Things aren't good at this point*
control.ID = "myUserID";
At the indicated point *, MyCustomControl already has it's UniqueID
set and it is "myCustomID" instead of "myUserID_myCustomID", or
"_ctlX_myCustomID" if I never plan to explicitly set the UserControl
ID. If I take the Attribute setting of CustomProp1 out of the ascx
and move it to the code behind of MyUserControl, everything works
fine. I think the difference is that OnInit gets fired first on
MyUserControl if the property setting is done in the code behind, but
EnsureChildControls in MyCustomControl gets called first if the
property setting is in the ascx. This causes the UniqueID of
MyCustomControl to get set before it's parent's UniqueID, making it
bogus. That's my guess anyway. It's definitely bogus at the time
it's needed to tie up events.

Has anyone else encountered this situation? Is there a clean way to
fix it? I'm looking for a best practice or something. I really need
EnsureChildControls to be in all the property settings or things blow
up all over, but I also need to be able to load UserControls to be
placed in WebForms, and set properties inside of those UserControls in
the ascx files. These two relatively simple things seem to conflict
as far as timing due to properties being set.

Any help will be greatly appreciated.
Nov 18 '05 #1
1 2237
I've done some more work on this. I created a simplified scenario to
try and duplicate the above situation. Unfortunately, I was unable to
duplicate the problem, but here is what I found. Setting the property
in the ascx does indeed cause the custom control to get created before
the UserControl, and the UniqueID at this point is indeed incorrect.
The real kicker though is that as soon as you enter OnInit for the
UserControl, the UniqueID of the custom control gets magically
corrected. I have no idea how this happens.

The bad part is that this DOES NOT happen in my big project. Because
I don't know how it happens in my smaller debugging project, I'm at a
loss to try and figure out how to fix it. The bigger project has more
layers of UserControls in UserControls, but they all derive from
System.Web.UI.UserControl. All Custom Controls end up deriving from
both System.Web.UI.Control and also INamingContainer.

If anyone knows more about the following - custom controls that get
their CreateChildControls override called before OnInit of their
parent, which causes their UniqueID to get set incorrectly initially,
then somehow have their UniqueID get corrected by the time OnInit in
their UserControl gets called. If you understand how the UniqueID
gets corrected, please let me know because it is simply not happening
in my large project. Every custom control that has properties set on
it in the ascx will forever be doomed to have the wrong UniqueID, and
therefore, no event wireups.
Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ari Royce Hidayat | last post: by
reply views Thread by Ari Royce Hidayat | last post: by
1 post views Thread by Sally | last post: by
5 posts views Thread by Dimitris Pantazopoulos | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.