tags:

views:

167

answers:

5

I have to hide the VIEWSTATE and EVENTVALIDATION hidden fields on my ASP.net page at RUN time.

I managed to remove the EVENTVALIDATION like so.............

<%@ Page enableEventValidation="false" EnableViewState="false" %>

But the VIEWSTATE is still there and I cant get rid of it and I need to. (hard to explain why)

Is there another way of getting rid of it?

Thanks in advanced!

+1  A: 

To remove the __VIEWSTATE altogether, you'll need to get rid of the <form runat="server"> tag. Of course, you won't be able to use controls that require rendering in a server form.

Mehrdad Afshari
Ai damn, yeah I need that as I am making use of ASP.NET controls.
Etienne
@Etienne: That's the cost you're paying for the stateful framework you're getting with Web Forms. By the way, you can use many ASP.NET controls outside a server side form. Not all of them require this.
Mehrdad Afshari
@Ralph: some people call it the "ZeroForm" approach: http://madskristensen.net/post/WebForms-or-MVC-What-about-the-third-option.aspx
Mehrdad Afshari
Interesting. I hadn't seen that before. Probably not used much. I didn't even think the Repeater and PlaceHolder controls worked without the runat server tag! Live and learn...
Ralph Stevens
@Ralph: In fact, the default view engine that ASP.NET MVC uses is just ASP.NET Web Forms. It simply cannot use controls that require view state but you can certainly use a control like `Repeater` in ASP.NET MVC.
Mehrdad Afshari
By the way, `Repeater` doesn't work without `runat=server` attribute on itself, which is fine. The point is, it doesn't need to be placed inside a server form.
Mehrdad Afshari
A: 

Or you can use ASP.NET MVC. It does not use _VIEWSTATE.

Ralph Stevens
ASP.NET MVC doesn't really do anything special in that regard. It simply doesn't use `<form runat="server">`. If you add that (it's kind of stupid to do), you'll get a `__VIEWSTATE` field there too.
Mehrdad Afshari
MVC is a completely different (and cool in my opinion!) way of doing things that does not depend on web forms and therefore doesn't need the <form runat="server">.
Ralph Stevens
I think ASP.NET MVC is very relevant to the question in that it was designed for traditional HTML/HTTP forms without the use of view/control state.
Richard Szalay
@Ralph: Nobody ever forced you to use `<form runat="server">` in Web Forms. I've been doing this for years before MVC.
Mehrdad Afshari
A: 

In ASP.NET 2.0, __VIEWSTATE holds both View State and Control State with the major difference being that a control can work properly over multiple postbacks without View State, but not Control State.

The only solutions are, as Mehrdad and Ralph mentioned, to remove the <form runat="server"> (making it an HTML form) or to use ASP.NET MVC. Both involve not using a server-side form, but ASP.NET MVC was created with the intention not to use any server side controls.

For more information on view/control state, see ASP.NET State Management Overview

Richard Szalay
+2  A: 

You need to override the following methods on your page:

protected override void SavePageStateToPersistenceMedium(
object viewState)
{
}

protected override object LoadPageStateFromPersistenceMedium()
{
    return null;
}

See here : http://weblogs.asp.net/ngur/archive/2004/03/08/85876.aspx You may be able to use this code to rename the viewstate variable if you wish but i would advise against that.

Removing the runat="server" from the form will also work as others have suggested.

Without the viewstate you will be unable to use asp.net controls. You may need to rethink what you are trying to do and why you need to get rid of the viewstate section.

Kaius
A: 

Please see this answer. However, if you don't want the viewstate, the by far best way to get rid of it is to use MVC.

erikkallen