Hi All,
I'm having a problem with a form I'm building which contains dynamic controls.
Basically, a user can select a category, on selecting that category a number of questions are retrieved from the database and are displayed on the form, either as textboxes or listboxes. If they change the category, then the existing controls are removed and new ones displayed. This works beautifully, until I add an event handler to a dynamic listbox. I need to do this as if the user than selects an option from a listbox, it may need to display another control depending on the answer they gave.
So, when adding the new controls, I use the following code:
listbox.SelectedIndexChanged += new EventHandler(extraQuestion_SelectedIndexChanged)
Now, when I select a different category, I get an error saying "Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the last request". If I comment out the above line, it works fine again. It also works fine for textboxes, it just seems to be lists that it has problems with.
The controls are being added in the Page_Load event, so it checks what category is currently selected, retrieves questions for that category, and creates controls for each question retrieved.
Any help would be much appreciated.
I'm having a problem with a form I'm building which contains dynamic controls.
Basically, a user can select a category, on selecting that category a number of questions are retrieved from the database and are displayed on the form, either as textboxes or listboxes. If they change the category, then the existing controls are removed and new ones displayed. This works beautifully, until I add an event handler to a dynamic listbox. I need to do this as if the user than selects an option from a listbox, it may need to display another control depending on the answer they gave.
So, when adding the new controls, I use the following code:
listbox.SelectedIndexChanged += new EventHandler(extraQuestion_SelectedIndexChanged)
Now, when I select a different category, I get an error saying "Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the last request". If I comment out the above line, it works fine again. It also works fine for textboxes, it just seems to be lists that it has problems with.
The controls are being added in the Page_Load event, so it checks what category is currently selected, retrieves questions for that category, and creates controls for each question retrieved.
Any help would be much appreciated.