Geeks With Blogs

News Google

Nick Harrison Blog<Nick>.Next()

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 Stylish musings | Back to top


Comments on this post: View State not Tracking on Dynamic Controls until after the second post back

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
Thank you *so* much! I just spent three hours smashing my head against the LCD over a very similar bug.
Left by Josh on Jul 10, 2007 7:28 PM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
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 !!
Left by subhan on Dec 08, 2007 1:24 PM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
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
Left by ram on Dec 26, 2008 3:25 AM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
Ugh, Vista has all sorts of problems with it. Soooo buggy!
Left by Online Banking on Jul 06, 2009 10:09 PM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
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.
Left by Magic the Gathering on Aug 20, 2009 2:03 PM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
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 !!
Left by nokia 5310 on Sep 11, 2009 1:05 AM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
Dynamic control help make the coding so easy.. Dentist Birmingham
Left by Dentist in Birmingham on Nov 08, 2009 4:14 AM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
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
Left by Dental implants Birmingham on Nov 24, 2009 5:45 AM

# re: View State not Tracking on Dynamic Controls until after the second post back
Requesting Gravatar...
Thanks for the fantastic control bars.. i love it.
Left by Inman Aligner birmingham on Nov 24, 2009 5:46 AM

# post
Requesting Gravatar...
Nice to see this post where i like to discuss about this thanks for sharing the info. Thanks.
Left by investing in cambridge on Aug 07, 2010 5:55 AM

comments powered by Disqus

Copyright © Nick Harrison | Powered by: GeeksWithBlogs.net