I can’t seem to be able to disable ViewState for controls that I add to a page dynamically.
ASPX
<%@ Page Language='C#' AutoEventWireup='true' CodeFile='Page1.aspx.cs' Inherits='Page1' EnableViewState='false' %> ... <asp:PlaceHolder ID='DropDownPlaceHolder' runat='server' EnableViewState='false' /> <asp:Button ID='Submit' runat='server' OnClick='Submit_OnClick' Text='Click Me!' EnableViewState='false'/> ...
ASPX.CS
protected override void OnInit(EventArgs e) { DropDownList dropDown = new DropDownList(); TextBox textBox = new TextBox(); textBox.EnableViewState = false; dropDown.Items.Add('Apple'); dropDown.Items.Add('Dell'); dropDown.Items.Add('HP'); dropDown.AutoPostBack = true; dropDown.EnableViewState = false; DropDownPlaceHolder.Controls.Add(dropDown); DropDownPlaceHolder.Controls.Add(textBox); base.OnInit(e); }
If I can’t disable ViewState on these controls, then I can never programmatically override what a user has entered/selected.
I’ve tried placing this code in OnInit and Page_Load, but the effect is the same in either location — ViewState is enabled (the DropDownList maintains selected value and TextBox retains text that was entered).
So, how can I disable ViewState and keep it from populating these controls?
Thanks!
Thanks for your response. Unfortunately, this isn’t a workable solution in my situation.
I will be dynamically loading controls based upon a configuration file, and some of those controls will load child controls.
I need to be able to turn the ViewState of controls off individually, without the need to code logic for loading controls in different places (in OnInit vs. LoadComplete method).
It’s a ‘feature’ — http://support.microsoft.com/?id=316813
Thanks, Microsoft!!