ASP.Net, Dynamic Controls and ViewState, Oh My!
I've a page that loads a control
dynamically, based on a drop down list that the user selects. I want to add the
control to the page, and have it work like any other control on the page. A
dynamic control in ASP.Net doesn't persist between page post backs unless you
add it each time the page load. Now, here is an interesting problem. You can
add controls to the page just about whenever you want, but if you want their
view state and the form values to be loaded properly, it must be done in
OnInit().
So far so good, I thought. I had
just one problem. I needed to add the control based on the value of another
control, and I would get that until the view state / form values was
loaded. But if I waited that long, my dynamic control wouldn't be bound to the
form values that it should have.
My solution was to manually extract
the form value of the drop down list and create the control in the OnInit(),
the issue here about submitting the page. For some reason I'm losing the values
of the controls in the dynamic control. I always gets the old values. I
can't find out what is going on there, and I can't get a simple reproduction to
show the same problem. The worst part is that it is working consistently when
I've saving things, but not when I'm updating them.
I tried to re-create the same
situation in a sample project, but there it is working properly.
I burned two days on this thing
already, and I'm not sure how to solve the issue. I know that it's probably
just something that I'm missing with save / load, but I'm not sure what is
going on there. I read enough material about the
ASP.Net life cycle to suffice me for a lifetime, but I can't figure out the
answer. I think that this may have to do with the fact that the button that
generate the postback is outside the user control.
The values outside the user control update
normally, which make it all the more frustrating.
Comments
Comment preview