Peter's Gekko

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
AddControl (always?) wrecks havoc to your viewstate

In a base-webform I had been using a Controls.Add(at) to inject a literal control  in my baseform to apply a stylesheet. The bad thing is that this method can completely wreck the viewstate of the page. Does anybody know whether this is a bug or a feature ?

Thank goodness there is an alternative to addcontrol. Less flexible but good enough.

Peter

 


Posted Mon, Feb 2 2004 2:47 PM by pvanooijen

[Advertisement]

Comments

Scott Galloway wrote re: AddControl (always?) wrecks havoc to your viewstate
on Mon, Feb 2 2004 9:52 AM
Just the way ViewState works...it just identifies what control the state belongs to using the control's ID...if you inject dynamically in the control root (Page in the case of ASP.NET) you are going to screw up the Viewstate - since it's likely you'll modify the ids of following controls. Using a Placeholder (or Denis Bauer's Dynamiccontrolspalceholder - http://www.mostlylucid.co.uk/posts/740.aspx) can get over the issue - since you're adding further down the tree...
Scott Galloway wrote re: AddControl (always?) wrecks havoc to your viewstate
on Mon, Feb 2 2004 9:56 AM
Oh...just read the original post...hmm...you can have head with runat=server or even switch the stylesheet using Javascript OnLoad (not ideal though). Sorry!
Peter van Ooijen wrote re: AddControl (always?) wrecks havoc to your viewstate
on Mon, Feb 2 2004 2:39 PM
You can access the viewstate of a page per item from code. I hoped on a some preservation of what I had put in there. Changing Id's would mess things up, I agree on that.

So, I guess it is a feature ?
Martijn Boland wrote re: AddControl (always?) wrecks havoc to your viewstate
on Mon, Feb 2 2004 2:42 PM
The best place to add controls dynamically is in the OnInit or AddParsedSubobject events, so the controls are all in place before Viewstate is loaded. You'll notice everything works fine then.
Paul Wilson (http://www.wilsondotnet.com) has written a lot of nice stuff in this area.
Peter van Ooijen wrote re: AddControl (always?) wrecks havoc to your viewstate
on Mon, Feb 2 2004 2:58 PM
Killer !
Move the control insertion from OnLoad to OnInit and the viewstate is preserved. After Onit has completed the page starts tracking viewstate. OnLoad is fired after onit.
Thanks, Peter
Peter's Gekko wrote Adding controls at runtime, the viewstate and the page lifecycle
on Tue, Feb 3 2004 2:54 AM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us