p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/forumdisplay.php?f=57)
-   -   ViewState and ItemCreated (http://p2p.wrox.com/showthread.php?t=21433)

eoghan November 18th, 2004 08:11 AM

ViewState and ItemCreated
 
Scenario:
I have a datalist which gets databound when !IsPostBack

When IsPostBack:
A user may have edited some of the textboxes in it, so I want to get the updated values.
I do this with the OnItemCreated event by:
TextBox mybox = (TextBox)e.Item.FindControl("tbox_id");
mybox.Text;
Unfortunately, mybox.Text turns out to be empty, even though I have EnabledViewState on.
Subsequently, however, the textbox text is preserved (through the viewstate I presume), and shows up on the page, but this restoration of state seems to happen after the ItemCreated event.

Any ideas of how to get the state of controls from within the ItemCreated event?



planoie November 29th, 2004 01:06 PM

The items in your datalist (and subsequently the textboxes that are in each item) get recreated at the beginning of page creation. They also get created when you bind the control. On a postback, the control hierarchy (datalist, items, textboxes) is recreated from viewstate. However, their values are filled in by the postback handler which gets them out of the form's posted data. So during "ItemCreated" you may very well not see the data.

If you want to be able to harness the changed state of a textbox, you need to wire the textbox OnChange handler to a server side event. You can do this by adding an event handler to the textbox during the OnItemCreated event of the datalist. Then, when the page enters the "raise postback events" stage of the page lifecycle, any textbox that has actually changed will raise the change event and thus call the method you wired to it.


All times are GMT -4. The time now is 06:13 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.