tags:

views:

913

answers:

6

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!

A: 

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.

thismat
A: 

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

AlexWalker
A: 

When I had issues with viewstate this article helped immensely. http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx

nportelli
A: 

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

Thanks, Microsoft!!

AlexWalker
+2  A: 

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.

Aydsman
My understanding was you had to take special steps to enable a control to use ControlState - it doesn't just happen automatically...so perhaps this is not ControlState?
Sam Schutte
You need to take special steps to enable a control you are implementing to use ControlState.If the control which you are using has already been coded to use ControlState you don't have to do anything to enable it's use, it will just happen.
Aydsman
+2  A: 

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

Peter