Last post Apr 15, 2011 08:35 AM by Careed
Apr 11, 2011 06:02 PM|DKP1110|LINK
I've got a web page that includes a user control. The page represent an order and the user control represents the line items. Since the number of line items can be modified, the user control is programmatically creating an HtmlTable for formating and some
associated WebControls for each line. To add a line, I add an empty HtmlRow and the corresponding WebControls to the table. The question is how do I manage state for these programmatically created controls and how can I query their values on post back?
The EnableViewState property is set for all WebControls, but how to find these controls on post back? Is it valid to cache a pointer to a TextBox in session state when creating a page (i.e. will the pointer be valid on post back)? Do I have to save the values
of all of the controls into session state?
I've tried using FindControl() using both the ID's I gave and the ones that show up in the page source, but both return null.
Apr 11, 2011 08:37 PM|Careed|LINK
Where are you dynamically creating these controls? If you are creating the controls after the ViewState has been applied to the existing controls, then you will be missing your ViewState data. The same is also true for any input controls that use Form
Apr 11, 2011 09:46 PM|DKP1110|LINK
The initial controls (the read-only view of the first order) are created during Page_Load() of the parent page. However, after that they are invoked in repsonse to post backs (not in my Post_Load(), but afterwards when my event handler is invoked by ASP).
For example, when I hit the create button it resets the parent form fields to their default values and removes all of the items from the child table (since a new order has no line items). Then I can press the add button to add a row to the table, enter some
data, and then press commit. In this commit post back is when I want to read the contents of the line item controls.
At what point is the ViewState applied so that I can make sure my controls are created? If it is before my handlers are invoked, how can I deal w/ this?
Apr 12, 2011 08:44 PM|Careed|LINK
ViewState is loaded before the PreLoad event. Thus, if your controls are being initialized in the Load event, you have missed the ViewState.
Typically, you should initialize your controls, do it in the Init event.
Apr 13, 2011 11:08 PM|DKP1110|LINK
I tried this and I can see that it works. However, my follow on question is how does one deal w/ the case where the number of programmatically generated items can change? For example, let's say I start w/ 2 items in the list and I press the add button.
On the post back the "add" handler will be called after both Page_Init() and Page_Load(). I can add another row to the HtmlTable, but in order to add controls that are ViewState-ready, I need to have pre-created the row controls in Page_Init(). Do you always
just create 1 more than you need in case the add button is pressed? Do you just pre-allocate some arbitrary maximum? Just wondering if there is a common methodology for this?
Apr 14, 2011 01:02 AM|Careed|LINK
Note that the SaveViewState process occurs near the end of the lifecycle, around the PreRender event. Thus, if you're adding a feature between Page_Load and Page_PreRender, there shouldn't be any issues with this.
Apr 14, 2011 09:29 AM|DKP1110|LINK
Let me make sure I follow. Let's say I have a page and I press the 'add' button. During the subsequent post pack, in my event handler, I create a TextBox w/ ID='123' and add it to the page. Then I type some value in the TextBox and hit the 'submit' button.
Since the TextBox was created before Page_PreRender(), it is included in the ViewState. On the subsequent post pack I need to re-create a TextBox w/ ID='123' in Page_Init() so that when the ViewState is processed the value in my TextBox will be restored.
In my event handler, I can then read the value from the TextBox and process it as normal. Is that right?
I'm assuming the ID is what is used to match the VeiwState data to the controls between postbacks.
Apr 15, 2011 08:35 AM|Careed|LINK
Since the TextBox control corresponds to an HTML Input tag, then the information stored in the TextBox is actually maintained through a Form variable, accessible through Request.Form. Given that one difference, your description is an accurate summary of
the process for your TextBox.
In ASP.NET, the system still uses the built-in features of HTML so as not to increase the size of the ViewState value. Thus, all input-tag-based controls are not maintained through ViewState, but through the normal functionality of posting web pages to
a server. ASP.NET then processes all information at the same time, both ViewState and Form data.
Note, of course, that the ViewState value is storaged in a hidden input tag and is read as a Form variable upon post back to the server.