简体   繁体   中英

How can I disable ViewState for dynamically created controls?

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).

@This Mat

It seems the post(back) will send the selected value, even if a control has it's viewstate turned off.

Yes. The post will always send a controls selected/current value. That's a behavior of HTTP form posting and nothing to do with ASP.NET control/view state.

What you are seeing there is the ControlState which was introduced with .Net Framework 2.0. It is designed to store the absolute minimum information for a control to work, such as the selection in your DropDownList and the text in your TextBox . Properties which are only related to appearance, such as BackColor , are still persisted within the ViewState .

Unlike ViewState you can't turn ControlState off .

I can only assume this is to avoid confusion from end-users when controls do not do what they are supposed to if they disable ViewState without understanding the consequences.

It's a "feature" -- http://support.microsoft.com/?id=316813

Thanks, Microsoft!!

I tested this a bit and found it a little odd. It seems the post(back) will send the selected value, even if a control has it's viewstate turned off.

I did find a good solution around it though, is that if you handle everything after the init and load, you will reload your control after the viewstate has been processed and it will thus, reset the values.

I used the LoadComplete event, example below:

  Public Sub Page_In(ByVal sender As Object, ByVal e As EventArgs) _ Handles Me.LoadComplete Dim ddl As New DropDownList() ddl.EnableViewState = False ddl.Items.Add("Hello") ddl.Items.Add("Stackoverflow") phTest.Controls.Add(ddl) End Sub 

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM