EnsureChildControls causes UniqueID to be incorrect inside UserControl

K

Kepler

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:

MyCustomControl.cs
------------------
public string CustomProp1
{
set
{
EnsureChildControls();
// Set something here.
}
}

MyUserControl.ascx
------------------
<myTag:MyCustomControl id="myCustomID" CustomProp1="something"
runat="server" />

MyWebForm.cs
-------------
OnLoad(EventArgs e)
{
MyUserControl control = LoadControl("MyUserControl.ascx");
// Things aren't good at this point*
control.ID = "myUserID";
panelContent.Controls.Add(control);
}


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.
 
K

Kepler

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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top