Dynamic Control Event Wireup?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

In my ASP.NET page code-behind, I have added a textbox like this:

(C#)


System.Web.UI.HtmlControls.HtmlGenericControl div1= new
HtmlGenericControl( "div" );
div1.ID = "divNewTextBox";
TextBox txt1 = new TextBox();
div1.Controls.Add( txt1 );

Now I want to add the TextChanged event to the textbox as early as
possible so on a PostBack the wired-up event can fire. I think I need
to add the div and textbox before Page_Load, because by then ViewState
is already processed, is that correct?
 
adding it in the page_load also works. but recommended is to put the dynamic
creation of controls in init method.

Av.
 
Hi Localhost,

I agree with Avnrao's suggestions that both Page_load and the Init event is
ok to add the dynamic created control and wire their event handler( but we
need to add the control and wire event handler for it everytime the page is
loaded ). And as for the time when to wire up the event handler, the Init
or Load is both ok because as long as we wire up the event handler before
the page processing the serverside controls post back events(it is after
the Page_load) the event will be correctly fired and call the handler. For
detailed info on the asp.net page's serverside event life cycle, you may
view the following article:

#The ASP.NET Page Object Model
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-pageobjectmodel.a
sp?frame=true

And the following twos are the reference on creating dynamic controls on
asp.net webform:

#Adding Controls to a Web Forms Page Programmatically
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskaddingcontrolstowebf
ormspageprogrammatically.asp?frame=true

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/?id=317794

Hope also helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi Localhost,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top