Add dynamic ASP.NET user controls BASED ON ViewState - BUT before LoadViewState phase?

I'd suggest retrieving drop-down list value from the request directly using the UniqueID property.

Up vote 0 down vote favorite share g+ share fb share tw.

Setup: My page has a drop down whose values are dynamically populated. Based on which item is selected, a number of TextBoxes are dynamically created during runtime. The user then fills in information into the textboxes and clicks a submit button.

Problem: After postback from the submit button, I need to again dynamically create the TextBoxes during Page_Init (BEFORE LoadViewState) so that after the ViewState loads, my Button_Click event can save/do whatever with the user input. PROBLEM is, I cannot recreate the textboxes based on the selection in the dropdown, because the dropdown hasn't been "selected" yet by LoadViewState. SO, how can I read from the view state, create my textboxes, then let the viewstate populate the textboxes, and then the Button_Click will use the values?

The one thing I've attempted is to override the LoadViewState function so that I can read from the view state, create the boxes, and then load the viewstate again. This did NOT work, because the debugger never seemed to hit my overridden function. Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.

LoadViewState(savedState) //'Do something like add controls Dim test As String = RecordList. SelectedValue //'Create controls using value "Test" MyBase. LoadViewState(savedState) End Sub Any help would be appreciated.

I can post more code if needed. Thanks, David asp.net vb.net usercontrols dynamic viewstate link|improve this question asked Jan 27 '11 at 2:50Dave105.

– TheGeekYouNeed Jan 27 '11 at 8:57 It's a full postback, and the dropdown values are originally populated in a separate event, but then carried across postbacks by ViewState – Dave Jan 27 '11 at 18:35.

I'd suggest retrieving drop-down list value from the request directly using the UniqueID property: Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Dim selectedValue As String = Request. Form(RecordList. UniqueID) ' Recreate your dynamic controls based on the selected value End Sub Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) ' Examine dynamic controls and their values (retireved from the ViewState) End Sub.

Volpav, this works perfectly. Thank you very much! – Dave Jan 27 '11 at 18:43 Side note, is this a work-around, is is this the proper solution?

I will be using it, regardless :-) – Dave Jan 27 '11 at 18:43 @Dave: This is just how the ASP. NET manages the life cycle of the page: msdn.microsoft.com/en-us/library/ms17847.... Your case is quite unusual since the state of your dynamic controls (that are being created BEFORE the ViewState is loaded) depends on another control's ViewState (that is being loaded AFTER all dynamic controls are added into the control tree).

But yes, I'd rely on this solution (don't seen any drawbacks related to your case) ;-) – volpav Jan 28 '11 at 7:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions