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

Retrieving values from dynamically added controls

P: n/a
I have a custom control that I wrote (I inherit from
System.Web.UI.WebControls.CompositeControl). I dynamically add this control
to my Page, but I was told that dynamically added controls do not survive
postback. This seems to be true. However, this seems to make dynamically
adding controls rather pointless to me, since if you cannot retrieve the
values of the controls when you perform a postback, why add the controls in
the firstplace? Can someone help me figure out how to retrieve the property
values of a dynamically added control? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Sep 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nathan Sokalski wrote:
I have a custom control that I wrote (I inherit from
System.Web.UI.WebControls.CompositeControl). I dynamically add this
control to my Page, but I was told that dynamically added controls do
not survive postback. This seems to be true. However, this seems to
make dynamically adding controls rather pointless to me, since if you
cannot retrieve the values of the controls when you perform a
postback, why add the controls in the firstplace? Can someone help me
figure out how to retrieve the property values of a dynamically added
control? Thanks.
They do not survive postback on their own. You have to help them.

You can do that by re-creating them dynamically after the postback.
On top of that, you have to make sure that they get the same ID before and
after the postback.
If not, they will not respond to any events that they were supposed to
handle.

Usually, this means that in the page, you have to keep track of the number
of controls that were added dynamically.

--

Riki
Sep 9 '07 #2

P: n/a
If you re-create dynamic controls in Page_PreInit event, they will get their
properties populated correctly in Page_Load event.

Read through this:
http://msdn2.microsoft.com/en-us/library/ms178472.aspx

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:e6*************@TK2MSFTNGP06.phx.gbl...
>I have a custom control that I wrote (I inherit from
System.Web.UI.WebControls.CompositeControl). I dynamically add this control
to my Page, but I was told that dynamically added controls do not survive
postback. This seems to be true. However, this seems to make dynamically
adding controls rather pointless to me, since if you cannot retrieve the
values of the controls when you perform a postback, why add the controls in
the firstplace? Can someone help me figure out how to retrieve the property
values of a dynamically added control? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Sep 9 '07 #3

P: n/a
On 9 Sep, 04:23, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
I have a custom control that I wrote (I inherit from
System.Web.UI.WebControls.CompositeControl). I dynamically add this control
to my Page, but I was told that dynamically added controls do not survive
postback. This seems to be true. However, this seems to make dynamically
adding controls rather pointless to me, since if you cannot retrieve the
values of the controls when you perform a postback, why add the controls in
the firstplace? Can someone help me figure out how to retrieve the property
values of a dynamically added control? Thanks.
I hope I'm not at cross purposes here but I don't think that is true.
I too have been faced with the problem of creating web server controls
dynamically and then having to work out how to handle postback events,
or (in the case of check boxes) how to detect their state.

For me the answer was to read the Key/Value pairs contained in
Request.Form

For this to work it's important to assign a value to the ID field of
the control when creating it, else it will indeed get lost.

Admittedly I wasn't working with custom controls but I imagine the
principle is the same based on inheritance from base classes.

Phil Hall

Sep 9 '07 #4

P: n/a
Create dynamic control each time (Postback or First time) in Page Init
event to load values for control from viewstate or let you use
viewstate.Then store all these controls into a object array.

Now In case of postback in Button Click event, you can loop through
controls object array you made while creating these controls. Where
you can get all chabged calues of these controls.

For example:

in VB .Net

For Each ctrl As Object In objCtrls
If (Not Nothing Is ctrl) Then
'Expecting all tab controls will have save button
inside them.
ctrl.Save()
End If
Next

Hope it will help you, Lemme know if you need clarifications.

Regards,
AKS
http://www.aksClassifieds.com
http://forums.aksClassifieds.com

Oct 10 '07 #5

P: n/a
As the previous poster mentioned, the short of it is, you need to create the
dynamic controls in the Page_Init event. You also need to be sure you give
them the same ID (I believe). If you do that, then later in the page
lifecycle (like in Page_Load or any other events) the control will contain
its postback value.

If you're going to add controls to a page dynamically, it would really be a
good idea to get a firm grasp on the page life-cycle - how controls are
created in Page_Init, how they are populated with postback values, etc.
Actually, it's a good idea to know that stuff even if you don't use dynamic
controls.

There are some pretty good articles out there on the "how's" and "why's" of
dynamic controls. Here's a start:

http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
Hi! Can anyone please explain this for a textbox.text value? After I click
a
button, all the controls disappear, and also the textboxes and their
values,
which I need in the next process.
Nov 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.