For several days,  I have struggled with a problem with the view state on dynamically loaded controls.   I am using a fair number of dynamic controls on a little project that I have been working.   Everything was working fine except in one section.    In this one section,  view state was not being tracked until after the first post back.   Events were not responding until after the first post back.    Everything seemed to be setup properly and it was working everywhere else.   

Very perplexing.    Who ever heard of a problem fixing itself like that?    I have heard of dynamic controls NOT working after the second post back.    This was really strange.   Here I had obviously made some mistake in wiring up the dynamic controls that fixes itself after one post back.

I tried every thing I could think of to track this down.    I overrode the LoadViewState method, PreRender, SaveViewState, UnLoad, etc.    Everything looked good.   Everything was behaving as expected after the initial post back.      As hard as I looked,  I couldn't see anything different between the sections that worked and the area that was misbehaving.    A fresh set of eyes was needed.

Sometimes you can be too close to a situation to see what is in front of you.

I knew that it was important that the dynamic controls get the same ID when they are reloaded as they had when initially added.   I took great pains to reload each control in the same order.    I took great pains to unload controls that were no longer needed before adding any additional controls.    What I failed to notice and only a fresh set of eyes could see was that  in one place, coincidently the place having problems,  I incremented the control count,  added the control to the containing control's control array, and finally set its ID, using the control count.   Everywhere else,  I incremented the control count as the last step.  

This means that for the problematic section, there would be no control 0 when the controls are originally loaded.   Because I was incrementing the control count before using it, the first control would always have an ID of 1.   On post back when I reloaded the dynamic controls, the control would load but be given an ID of 0 because I was using the control count and then setting it.   There was no viewstate for Control 0 and there was no longer a control for the viewstate data associated with the control with ID of 1.    After the first post back, the gap in the control ids was removed and from then on everything worked properly.

I hope that my struggles help someone else.

posted on Monday, November 28, 2005 7:14 PM | Filed Under [ Stylish musings ]
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
Posted by Josh
on 7/10/2007 7:28 PM
Thank you *so* much! I just spent three hours smashing my head against the LCD over a very similar bug.
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
Posted by subhan
on 12/8/2007 1:24 PM
You rock buddy!! i was banging me head from 3 days on the same kind of issue..

i really appreciate your time..and patience in throwing light on this issue..

once again thanks !!
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
Posted by ram
on 12/26/2008 3:25 AM
Thanx a lot man. U saved my life two.I was having similar issue with dynamic rows added in gridview. I was getting data of dynamic rows only when user clicked the save button second time. For first postback i was not getting any data. The problem was i created all the row object and added controls to them but i didnt gave ids to any of these objects. May be my case will also some day help some one. So I am posting here to help others and to thank you. Thanks a lot!!! :P
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
Posted by Online Banking
on 7/6/2009 10:09 PM
Ugh, Vista has all sorts of problems with it. Soooo buggy!
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
on 8/20/2009 2:03 PM
I was getting data of dynamic rows only when user clicked the save button second time. For first postback i was not getting any data.
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
Posted by nokia 5310
on 9/11/2009 1:05 AM
You are just amazing Man !! I was tried of looking about it from websites to websites. I think it's my pleasure to leave a comment on your note. Your struggle is worth appreciating !!
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
on 11/8/2009 4:14 AM
Dynamic control help make the coding so easy.. Dentist Birmingham
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
on 11/24/2009 5:45 AM
It’s a valuable experience to be taking part in the blogs. You will know somany others and what they think on different topics. Blogs are the gateway to learning and beyond. Thanks
Gravatar
# re: View State not Tracking on Dynamic Controls until after the second post back
on 11/24/2009 5:46 AM
Thanks for the fantastic control bars.. i love it.
Gravatar
# post
on 8/7/2010 5:55 AM
Nice to see this post where i like to discuss about this thanks for sharing the info. Thanks.
Post Comment
Title *
Name *
Email
Url
Comment *