.NET Forum / ASP.NET / General / January 2006
PreInit event in usercontrol
|
|
Thread rating:  |
Marcelo Cabrera - 22 Dec 2005 20:06 GMT Hi,
I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same time. I have to click twice on the controls for the events to be raised, the first time nothing happens, the second time the event handler fires up.
Reading the msdn info it says that for pages, the dynamically created webcontrols should be created in the PreInit event of the page, but usercontrols do not have that event, what is the life-cycle for usercontrols.
Also the usercontrol is dynamically loaded into the page at the Load event.
Thanks, Marcelo Cabrera.
Steven Cheng[MSFT] - 23 Dec 2005 03:50 GMT Hi Marcelo,
Welcome to ASPNET newsgroup. Regarding on the dynamic creating webcontrols in UserControl or in asp.net 2.0 web page problem, here are some of my understanding and suggestion:
1. For dynamic webcontrols, it's ok to create them in Page's Load or Init event (Init is the prefered one), this is also the recommendation in asp.net 1.1:
#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET http://support.microsoft.com/kb/317794/en-us
2. For asp.net 2.0, the reason why we recommend that put dynamic controls creation code in PreInit event is the ASP.NET 2.0's Theme/Skin service is applying skin to controls before Init event, so we need to add dynamic controls in page's controls structure in PreInit so as to automatically utilize the page's Theme setting.... But if we create them in Init or Load, we can also manually apply page theme for control through Control.ApplyStyleSheetSkin() method , e.g:
=================== TextBox txt = new TextBox(); txt.ID = "txtUC"; txt.AutoPostBack = true; txt.TextChanged += new EventHandler(txt_TextChanged);
phControls.Controls.Add(txt); txt.ApplyStyleSheetSkin(Page); ====================
For your scenario, I think the event handler problem may caused by how your Usercontrol (the container) is created and added into page's collection. Is the usercontrol always created and added into page in page's Init or Load event ?
If possible, would you provide a simplfied page and the usercontrol so that we can have a look into the detailed code logic?
Thanks & Merry Christmas!
Steven Cheng Microsoft Online Support
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
--------------------
| From: "Marcelo Cabrera" <marcelocabrera@noemail.noemail> | Subject: PreInit event in usercontrol [quoted text clipped - 10 lines] | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.aspnet:366633
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | [quoted text clipped - 17 lines] | Thanks, | Marcelo Cabrera. Steven Cheng[MSFT] - 29 Dec 2005 10:19 GMT Hi Marcelo,
How are you doing on this issue or does my last reply helps you a little? If there're anything else we can help, please feel free to post here.
Regards,
Steven Cheng Microsoft Online Support
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
--------------------
| X-Tomcat-ID: 104339147 | References: <#GPhcLzBGHA.2920@tk2msftngp13.phx.gbl> [quoted text clipped - 11 lines] | Path: TK2MSFTNGXA02.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.aspnet:366711
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122 | [quoted text clipped - 87 lines] | | Thanks, | | Marcelo Cabrera. Marcelo Cabrera - 01 Jan 2006 20:11 GMT Hi Steven,
I was on vacation last week, thanks for the reply. I don't think the problem could be solved without a way to add the handlers for the controls created dynamically. In other words, what's the equivalent of preinit for a usercontrol?
The thing is, I have an .ASCX page with a placeholder which in turn gets populated with a few usercontrols in the codebehind, then, those usercontrols have their own events and eventhandlers which in turn may load some other usercontrols during the pageload event of the usercontrols. When that happens the events in the second tier usercontrols don't fire until the second time the user clicks. It all used to work well on 1.1 but with 2.0 something broke, I tried moving the controls creation and event wiring to the pageinit as stated in the documentation (for ascx) but I can't find info about the page life cycle for usercontrols.
The usercontrols are always created and added to the page(s) during the load and/or preinit events.
Thanks in advance, Marcelo Cabrera.
> Hi Marcelo, > [quoted text clipped - 125 lines] > | | Thanks, > | | Marcelo Cabrera. Steven Cheng[MSFT] - 03 Jan 2006 10:29 GMT Thanks for your response Marcelo,
So I can currently get that you dynamically load some container userControls in the aspx page. Then, in those container usercontrol's certain postback events, you create and load some other sub usercontrols, and add event handlers for those sub usercontrols? What's the event, your custom defined events or buildin events?
Anyway, Usercontrols dosn't have PreInit event (different from Custom Server control...), we have only Init , Load event for usercontrol where we should put the dynamic child controls creation code....
Thanks,
Steven Cheng Microsoft Online Support
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
--------------------
| Thread-Topic: PreInit event in usercontrol | thread-index: AcYPD30JIUMlpHo6Sn+47FEA1e5jkw== | X-WBNR-Posting-Host: 69.203.154.124 | From: =?Utf-8?B?TWFyY2VsbyBDYWJyZXJh?= <marcelocabrera@noemail.noemail> | References: <#GPhcLzBGHA.2920@tk2msftngp13.phx.gbl> <zmKTXQ3BGHA.1240@TK2MSFTNGXA02.phx.gbl> <8oHHjFGDGHA.832@TK2MSFTNGXA02.phx.gbl>
| Subject: RE: PreInit event in usercontrol | Date: Sun, 1 Jan 2006 12:11:02 -0800 [quoted text clipped - 13 lines] | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.aspnet:367960
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | [quoted text clipped - 150 lines] | > | | Thanks, | > | | Marcelo Cabrera. Marcelo Cabrera - 03 Jan 2006 15:43 GMT Steven,
Yes, you got it right. the events are builtin events, when I need to bubble-up events to the parent usercontrol (for example to remove the child control) that works fine, simple built-in events like button onclick are the events that need to be triggered twice before getting processed.
Thanks, Marcelo Cabrera.
> Thanks for your response Marcelo, > [quoted text clipped - 223 lines] > | > | | Thanks, > | > | | Marcelo Cabrera. Steven Cheng[MSFT] - 04 Jan 2006 10:10 GMT Thanks Marcelo,
Would you provide me a simplified page and usercontrol with some buildin controls(button or textboxes) that are dynamicall loaded and bind event handlers so that I can perform some test on them?
Regards,
Steven Cheng Microsoft Online Support
 Signature Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
--------------------
| Thread-Topic: PreInit event in usercontrol | thread-index: AcYQfGJFjaOxVUPER0uVE2KgkpW00A== | X-WBNR-Posting-Host: 216.213.25.254 | From: =?Utf-8?B?TWFyY2VsbyBDYWJyZXJh?= <marcelocabrera@noemail.noemail> | References: <#GPhcLzBGHA.2920@tk2msftngp13.phx.gbl> <zmKTXQ3BGHA.1240@TK2MSFTNGXA02.phx.gbl> <8oHHjFGDGHA.832@TK2MSFTNGXA02.phx.gbl> <2AD8447C-EC54-41A0-BE42-FD0F6F71F9CE@microsoft.com> <XD4aiCFEGHA.1548@TK2MSFTNGXA02.phx.gbl>
| Subject: RE: PreInit event in usercontrol | Date: Tue, 3 Jan 2006 07:43:03 -0800 [quoted text clipped - 13 lines] | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.aspnet:368279
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | [quoted text clipped - 235 lines] | > | > | | Thanks, | > | > | | Marcelo Cabrera. bjossi - 16 Jan 2006 11:48 GMT Hi!
I have the same problem with UserControls. I have to click twice on the buttons on the control to make them react correctly. It seams to be the "doPostBack" that, by some reason, isn't correct. The scenario is as follows. The page load and 4 user controls are created all with buttons "edit" and "delete" the "doPostBack" for the first control has '_ctl1$...' the next has '_ctl2$...' etc (notice the numbers 1, 2.. etc). When I click the edit button on the first control the second control reacts. by displaying the "edit" box. But now, when I look at the "doPostBack" for the buttons the first control has '_ctl0$...' the second has '_ctl1$...' etc (the numbers start from 0) and now the buttons work for correct control. This implies that there is a problem with associating the correct control Id with the controls the first time they load. Why it is so I'm not able to find out.
Dose anyone have an idea how to solve this (have you Marcelo solved your problem) ???
Thanks... Björn Erlendsson
Marcelo Cabrera skrev:
> Hi, > [quoted text clipped - 15 lines] > Thanks, > Marcelo Cabrera. Marcelo Cabrera - 19 Jan 2006 16:18 GMT I "fixed" it by loading some javascript after processing the event. The javascript code will produce another 'postback' wihtout defining the control who did it, then when the user click on a control is like if it was clicked twice.
<asp:panel id="refreshscript_panel" visible="False" runat="server" enableviewstate="False"> <script language="javascript" type="text/javascript"> //__doPostBack('',''); document.forms[0].submit(); </script> </asp:panel>
just show the panel after the code handling events in your control and you will be ok, but beware that this will produce two postbacks.
> Hi! > [quoted text clipped - 40 lines] > > Thanks, > > Marcelo Cabrera. bjossi - 16 Jan 2006 11:49 GMT Hi!
I have the same problem with UserControls. I have to click twice on the buttons on the control to make them react correctly. It seams to be the "doPostBack" that, by some reason, isn't correct. The scenario is as follows. The page load and 4 user controls are created all with buttons "edit" and "delete" the "doPostBack" for the first control has '_ctl1$...' the next has '_ctl2$...' etc (notice the numbers 1, 2.. etc). When I click the edit button on the first control the second control reacts. by displaying the "edit" box. But now, when I look at the "doPostBack" for the buttons the first control has '_ctl0$...' the second has '_ctl1$...' etc (the numbers start from 0) and now the buttons work for correct control. This implies that there is a problem with associating the correct control Id with the controls the first time they load. Why it is so I'm not able to find out.
Dose anyone have an idea how to solve this (have you Marcelo solved your problem) ???
Thanks... Björn Erlendsson
Marcelo Cabrera skrev:
> Hi, > [quoted text clipped - 15 lines] > Thanks, > Marcelo Cabrera.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|