How to transfer control to an ASPX page using Javascript
I wrote a login form calling a WebMethod for authentication. This part is working fine.
If the user has been authenticated, I want to transfer control to another form using javascript. Following is my code:
var f = document.getElementById("form1");
f.action = "http://localhost/demo/WebForm2.aspx";
f.method = "POST";
f.submit();
I am getting the following exception:
[ViewStateException: Invalid viewstate.
Client IP: 127.0.0.1
Port: 2614
Referer: http://localhost/demo/authenticate.aspx
Path: /demo/WebForm2.aspx
User-Agent: Mozilla开发者_C百科/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
ViewState: /wEPDwUJNzgzNDMwNTMzZGRVwSzsPTf15ks/Fy9lgs6EmnjDEeWgjaAnQ01MZDLGJg==]
Anybody know what I'm doing wrong?
The error occurs because the page receiving the postback is trying to use the Viewstate data to populate the page it properties, but it is invalid, as the viewstate was created by another page.
You can disable the viewstate if there is no need, which will solve your problem.
private void Page_Init(object sender, System.EventArgs e)
{
...
this.EnableViewState = false;
...
}
This will make you handle the form post variables by yourself, instead the page variables being populates by the viewstate data, as usual in asp.net postbacks.
EDIT:
The viewstate
is a serializable data that represents your page state before it is rendered. It is useful for populating the properties in your page after you post the data back to your server, so that when you access those properties it would appear that you are not working in a client-server environment. Viewstate
is an awful hack among a bunch of other inside asp.net architecture. You should be careful with viewstate as it increases overhead like hell if you have a lot of properties set in your asp.net page class.
The problem is the __ViewState field. ASP.NET tries to validate the __ViewState contents and when you change the destination on the client you confuse it. This page describes what to do in detail: http://www.codeproject.com/KB/aspnet/jsnopostback.aspx
精彩评论