|
Adding controls dynamically to a page in ASP.NET
ASP.NET added viewstate in an attempt to persist state for controls across postbacks. Viewstate does what it's supposed to, but also presents some challenges when adding controls dynamically. If you've ever tried to modify the control hierarchy before viewstate has fully loaded, you've prolly seen this error:
"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 previous request."
For example, if I dynamically add a textbox to a page, that textbox MUST exist before the viewstate for the page is loaded after a postback. This is because the when the page attempts to load viewstate, the control hierarchy must match the control hierarchy that existed when viewstate was originally created.
So when you add a control dynamically, it must exist during the postback - at least long enough for viewstate to be loaded properly. This usually involves re-adding any dynamic controls that were present at last page load. What this means is that you can dynamically add, update and remove controls, but only at certain times in the page's lifecycle. Here is a breakdown of a typical page lifecycle:
Note: I'm omitting events and methods invoked by IPostBackDataHandler and IPostBackEventHandler for the purpose of this article.
- Instantiate - by constructor.
- Initialize - This is when the markup code is initialized into the control hierarchy. The Init event is fired and OnInit is called.
- Load Viewstate - The LoadViewState method is called.
- Load - The Load event is fired and the OnLoad method is called.
- Pre Render - The PreRender event is fired and the OnPreRender method is called.
- Save Viewstate - The SaveViewState method is called
- Render - The Render method is called.
- Unload - The Unload event is fired and the OnUnload method is called.
Given the above series of events, dynamically added controls should be added before the LoadViewState method is called and they must be added in the exact same order they were added on the previous page load. If they are out of order, or a control is missing, you'll get that stellar exception above.
So, when do you do it? Well, to me, Initialization seems to be the best place. Add the controls back in during the OnInit method. Let viewstate completely load itself back up, then do whatever you want... add more controls, remove existing controls, etc. Just make sure you add/remove controls before SaveViewState is called - this shouldn't be a problem to most since the only way to eff this up would be during the Render method - and if you're messing with the control hierarchy here, then you prolly shouldn't be reading this article.
Knowing the page's lifecycle of events and the timing of critical methods is paramount to getting dynamic controls to work. If you don't learn it well, you'll spend ten fold the amount of time fixing related bugs - and to me, these are the most frustrating to debug.
|
New Post Notification
Search Posts
Recent Posts
Lifextender v0.9.3.0 Available for Download
Visual Studio 2008 Add-in Compatibility
Lifextender v0.9.2.9 released
Introducing Lifextender - a commercial remover app for Vista Media Center
Commercial Remover app for Vista Media Center
Escape XML string characters in C#
MRU Cleaner for Orcas Beta 1
Property Manager Updated 1.0.0.4
Localizing existing strings into resource files is teh lame
Dispatch v1.0 Released.... finally.
MRU Cleaner v1.0.0.5 - Now serving your file-cleaning needs too
Explore in Windows Add-In updated - 1.0.0.2
MethodInvoker + Anonymous Methods = tEh r0x0r
Kick-ass Vista Firefox Theme
New Clipboard Manager Upgrade 1.0.0.7
Project MRU Cleaner 1.0.0.4 - Vista Ready and stupid dope
Firebug - My New Favorite Extension
Commercial Skip for Media Center Vista RC2
Dispatch v0.9.0.59 Released
New Remote-Overlay View Feature for Dispatch
New Version of Dispatch Released - v0.9.0.58
Remove Bookmark Custom Site Icons in Firefox 2.0
Dispatch beta 0.9.0.57 Available
About Meeself
People call me Bobby DeRosa
I live somewhere in San Diego, CA
MCSD, MCAD, MCP
|
|