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!