Disable viewstate

>re: What goes in ViewState when page EnableViewState property is set to false?
>I want to disable viewstate . How I can do that?
>Even the propery is false it's saving the state

As I wrote ASP.NET always write Page hash key to view state. To disable this behavior you need to override page SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium functions and to disable the default Page behavior by not calling page base class. Those functions persist viewstate data in hidden field.

 
protected override void SavePageStateToPersistenceMedium(

object viewState)

{

}

protected override object LoadPageStateFromPersistenceMedium()

{

   return null;

}

10 Comments

  • Not that I aware of ... :-)



    And I post it to ASP.NET newsgroup. it a shame that we design our programs in OO fashion to decrease code bloating / duplicating. but nobody ever think that way about the knowledge available in so many data "classes" without any "base class" to prevent data bloating/ duplicating ....

  • This solution will disable all controls viewstate but how we can disable viewstate for a single text box control and should be enable for other controls.

  • Thank you very much, i was going crazy trying to set the viewstate to false without any Success. I was exporting an aspx page to an xls file, but excel doesn't support a long viewstate value.

  • Cheats way below. Simply finds and rips out the viewstate tag from the rendered output:

    Dim ctrl As Control
    Dim stringBuilder As New System.Text.StringBuilder
    Dim stringWriter As New System.IO.StringWriter(stringBuilder)
    Dim htmlWriter As New HtmlTextWriter(stringWriter)

    For Each ctrl In Controls

    ctrl.RenderControl(htmlWriter)

    Next

    Dim html As String = Replace(stringBuilder.ToString(), "/>", ">")

    Dim intViewStart As Integer = InStr(html, "") + 1
    Dim strViewTag = Mid(html, intViewStart, intViewFinish - intViewStart)
    html = Replace(html, strViewTag, "")

    output.Write(html)

  • Forgot to say, put this in the following

    Protected Overrides Sub Render(ByVal output As HtmlTextWriter)

    End Sub

  • can u please write the above code in c#

  • C# version of Render ....

    protected override void Render(HtmlTextWriter output)
    {
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
    System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder);
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    foreach (Control ctrl in this.Controls)
    ctrl.RenderControl(htmlWriter);

    String html = stringBuilder.Replace("/>", ">").ToString();

    int intViewStart = html.IndexOf("", intViewStart) + 1;

    string strViewTag = html.Substring(intViewStart, intViewFinish - intViewStart);

    System.Diagnostics.Debug.WriteLine(strViewTag);

    html = html.Replace(strViewTag, "");

    output.Write(html);
    }

  • Thanks! that's exactly what I need!!!

  • Sorry, but what is this about disabling viewstate? It does NOT work!! I can PROVE it. Create an aspx page and put a button and a text box, then disable the textbox's viewstate, and the value entered in the text box IS persisted. So, there are so many holes in everyone's explanation, and it is ridiculous that you can't even explain my simple scenario. Theres no way that anyone really understands what viewstate is and how it is enabled or disabled.

  • This piece of writing is genuinely a pleasant one it helps new web visitors, who are wishing in
    favor of blogging.

Comments have been disabled for this content.