[thelist] (asp.net) accessing controls created at runtime.

James Conley Conleyj at kubota-kma.com
Tue Feb 21 11:21:14 CST 2006


The contents of the dynamically created controls are indeed stored in
viewstate.

The problem however comes into play onPost Back - the first thing
ASP.Net tries to do is to "restore viewstate" - that is - take control
information out of ViewState and put it into the contents of the
control. If the control does not exist then the ASP.Net
viewstate-restoration scheme fails. The solution is to create any
dynamic controls either in Page_Init or Page_Load.

james c


-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Peter Brunone
(EasyListBox.com)
Sent: Tuesday, February 21, 2006 11:40 AM
To: thelist at lists.evolt.org; thelist at lists.evolt.org
Subject: Re: [thelist] (asp.net) accessing controls created at runtime.

You're probably creating the controls *after* the click event handler.
Dynamically added controls aren't automatically re-created on postback,
and they're not implicitly stored in Viewstate (or so I understand).

You'll need to add your controls a la
Page.ViewState.Add("AttorneyFirstName", tbAttorneyFirstName) so you can
pick them up later.  Of course you could just use the Request.Form
collection which has every form element from the posted page, but
Viewstate is the common Microsoft answer (though I avoid it when
possible).

HTH,

Peter

P.S.  If you want a more ASP.NET-specific list, consider
http://groups.yahoo.com/group/AspNetAnyQuestionIsOK or some of the other
groups at http://www.aspfriends.com .  Not like you can't post here, but
you may get more specialized information there.




More information about the thelist mailing list