Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 18th, 2004, 08:11 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?


  #2 (permalink)  
Old November 29th, 2004, 01:06 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.


Similar Threads
Thread Thread Starter Forum Replies Last Post
viewstate error balesh.mind ASP.NET 2.0 Professional 1 August 31st, 2007 08:51 PM
ViewState Wervis C# 1 December 3rd, 2005 11:35 AM
Viewstate Wervis BOOK: ASP.NET Website Programming Problem-Design-Solution 0 November 30th, 2005 09:42 AM
ViewState abdul_owiusa General .NET 0 October 18th, 2005 04:37 PM
ViewState burhan ASP.NET 1.0 and 1.1 Basics 4 June 6th, 2004 04:25 AM





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