2

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

3
  • Are you using a callback or it's a full postback? Commented Jan 27, 2011 at 7:50
  • What event do you populate the dropdown values? Commented Jan 27, 2011 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 Commented Jan 27, 2011 at 18:35

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Side note, is this a work-around, is is this the proper solution? I will be using it, regardless :-)
@Dave: This is just how the ASP.NET manages the life cycle of the page: msdn.microsoft.com/en-us/library/ms178472.aspx. 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) ;-)
great solution. sometimes i forget about the HTML forms model after all these years of viewstate. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.