I’m new to ASP.net, the ViewState, and C# in general.
I have some code that adds and deletes boolean constraints that will later be submitted as an SQL query. I dynamically create these constraints with unique IDs. When I delete a constraint earlier that was added before some other constraint, the viewstate tries to map the contents of the DropDownList that was deleted to the DropDownList immediately below it.
This is turn triggers an index changed event and causes undesirable behavior in the application. I’m fairly certain that this behavior is caused because the ViewState loads the postback data into dynamically created controls via index.
I’ve been searching for a solution came across [ViewStateModeById] as a possible solution. Unfortunately, this seems to only to custom controls. I would like to avoid creating new custom controls for everything just to index the controls by ID.
Is there a way to gain this functionality without having creating custom controls?
I should mention that each dynamically generated constraint (which is a set of about 5 controls) is inside a dynamically generated panel. I could just keep creating empty panels as a sort of placeholder to get my desired behavior, but that is just a hack. I’ll end up with many, many panels unless I implement another hack to occasionally delete some of my panels.
I struggled with the viewstate until I got it. This is not a real answer but when working with viewstate it is very important that the dynamic controls created gets the exact same id after each post back. It means that you’ll have to re-create/re-bind everything on each post pack.
If it does I’d be surprised if you get strange behaviors and you should be able to code almost like if it was a desktop app.