473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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:

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.
Nov 18 '05 #1
1 2286
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ari Royce Hidayat | last post by:
Dear ALL, Is there some one ever faces this problem? And fix it? The scenario is: There's an html page that hosts a .net object (using object tag), and this page opens the second html page...
0
by: Ari Royce Hidayat | last post by:
Tx a lot for idea. I've tried it, and still get not clue what causes it. I also tried to see the log file generated by IEHost (by adding IEHostLogFile to registry) and still get not clue what...
1
by: Sally | last post by:
EnsureChildControls makes sure CreateChildControls() is called So then the children are "created" What does "created" actually mean? Is only Constructor() call? OnInit()? OnLoad()? ...? ...
2
by: Henri | last post by:
Sorry to post so many questions but if you could tell me the difference between these 3 IDs, it would help me a lot. I don't know which one to use inside my custom control...
5
by: Dimitris Pantazopoulos | last post by:
Hello. Are there any known issues with the EnsureChildControls method? In a composite control, the Width property wraps the Width property of the TextBox child control: public Unit Width {...
0
by: vbMental | last post by:
I'm almost certain this is a bug, or atleast "undesirable behavior". I felt the checkbox list control had shortcomings so I ventured on creating my own checkbox list user control. To put it...
0
by: vbMental | last post by:
I created a user control that, had a datalist with checkboxes inside of the item template. These checkboxes would not retain state. For instance, if I set them to checked inside of the load event...
14
by: multiformity | last post by:
So I have been working on an opensource project for a while, and decided to really try to make it look better after focusing on the functionality most of this time. Up to now, I have simply used a...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.