3

I have a site that is set up to validate the user from time to time. Every time the user is validated the user is redirected to the login page, which is another web application under IIS. Since the user is still valid it will be redirected back, but during this time it has lost the postback data making the whole form set to default.

My first thought was to just turn off view state on the form and use get instead of post on the form tag

<form runat="server" method="get" enableviewstate="false">...</form>

The get command works, but the querystring get the view state is printed making the url to long. Is there some easy to solve this? Basically what I want to do is to turn off viewstate completely, I've tried to use the enableviewstate, but I can't get it to dissapear.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137

3 Answers3

2

Have you tried setting the enableViewState property within web.config so you'd have something that looks like:

<pages enableViewState="false">
    ....
</pages>
1

You can disable viewstate across your application using Grant's suggestion. Alternatively, you could turn it off for a single page in the Page's declaration. For example:

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" EnableSessionState="ReadOnly" %>
Druid
  • 6,423
  • 4
  • 41
  • 56
0

The title contradicts the problem slightly, as it seems that your actual issue is that although you have set EnableViewState=False you are still getting the viewstate written to the page as hidden variables.

This question is along the same lines, but you still get hidden fields written even if you use these two methods:

Your own PageStatePersister:

public class EmptyStatePersister : PageStatePersister
{
    public EmptyStatePersister(Page page) : base(page) { }
    public override void Load() { }
    public override void Save() { }
}

protected override PageStatePersister PageStatePersister
{
    get
    {
        return new EmptyStatePersister(this);
    }
}

Your own page class, as the question linked describes:

public class EmptyViewStatePage : Page
{
    public override bool EnableViewState
    {
        get
        {
            return false;
        }
        set
        {
            base.EnableViewState = false;
        }
    }

    protected override void SavePageStateToPersistenceMedium(object state)
    {

    }

    protected override object LoadPageStateFromPersistenceMedium()
    {
        return null;
    }
}

So you're left with jQuery:

<script type="text/javascript">
    $(document).ready(function ()
    {
        $("#__EVENTVALIDATION").remove();
        $("#__VIEWSTATE").remove();
    });
</script>
Community
  • 1
  • 1
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • Why do I have to use javascript to change the form method? It is fully possible to just set the method in the form tag, but that doesn't solve my problem since the querystring still get to long. Also, I think you misunderstood my question... I don't want any viewstate at all, so if I can get rid of it the GET request would work. – Tomas Jansson Mar 31 '11 at 12:48
  • @Tomas So with no headers or querystring variables passed back to the server, how do you propose the data being sent? – Chris S Mar 31 '11 at 17:47
  • I don't know if I am unclear or you just don't understand me, maybe a mix of both :). I didn't say that I won't pass back any querystring variables or headers... All I'm asking for is to use get instead of post on a WebForm page, plus the option to leave out the viewstate which is added to the querystring right now if I use get since the `ViewState` variable is stored in a hidden field in the form. When the `ViewState` is added to the querystring the resulting querystring gets to long. – Tomas Jansson Apr 01 '11 at 08:04
  • 1
    @Tomas it can't be done, even using option 2. You always get a hidden field with _VIEWSTATE unfortunately. – Chris S Apr 03 '11 at 21:37