Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Failed to load viewstate - event handler on dynamic listbox

Status
Not open for further replies.

NottsRed

Programmer
Mar 6, 2009
3
GB
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.
 
dynamic controls, viewstate, and events are a tricky thing.

1. the dynamic controls should be added in the init event, no the load event.
2. the dynamic controls should be assigned an id so they are properly handled in viewstate
3. the dynamic controls and all associated events and properties must be assigned with each postback.

if you are adding removing controls with each postback this could be the problem.

another option is to put all the controls on the page and and show/hide them according to the user input.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks jmeckley.

I'll give putting it into the page_init a go, though I did try this yesterday without success (this could have been trying to fix something else though so I'll try again). There is quite a lot of other stuff going on too so it could take me a while to get it all working in the correct order again (ie. I'll have to put other bits and pieces into page_init as well).

Unfortunately putting all the controls on the page and hiding showing them wouldn't work due to the layout. I have 2 columns of fields, so when adding if its added 2 controls it then adds the table row and moves onto the next row, only adding the controls I need depending on the user selections. If I put them all on the page in the first place I would end up with gaps where the hidden fields are.

I do seem to have fixed it temporarily, but its not a fix I want to leave in as its a case of sticking a "try catch" around where the error occurs :) It does seem to still keep the view state, but I still don't trust it.

I'll let you know how I get on with the page_init.
 
There are many problems when using dynamic controls. Do you really need to add controls dynamically? Can you just use multiview and have different versions of the page you want to show? In other words, see if dynamic conrols are what you really need to use.
 
The main issue is that it's taking the fields from the database, and they could change (ie. I don't know how many views I'll need until runtime). We're trying to make the solution as adaptable as possible with minimum coding changes when we need to add new categories and questions, so the way its working now (well, I think it's working, still not sure about the try catch) will mean no coding changes at all.

It does appear to be working with the try catch statement, it always seems to be hitting the line of code that the error occurs on (which is adding a row to a table), but is no longer showing the error. The dynamic lists are also posting back without error, so the next step is taking the value from these dynamic listboxes, and generating more dynamic fields based on them, which is where the real fun starts :)
 
this is why I stopped using webforms basic html is so much simpler :)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top