By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Dynamic Control Event Wireup?

P: n/a


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?
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
adding it in the page_load also works. but recommended is to put the dynamic
creation of controls in init method.

Av.

"localhost" <pr*******@cohort.ces> wrote in message
news:mi********************************@4ax.com...


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?

Nov 18 '05 #2

P: n/a
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...eobjectmodel.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...controlstowebf
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



Nov 18 '05 #3

P: n/a

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

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.