简体   繁体   English

如何使用Javascript将控件转移到ASPX页面

[英]How to transfer control to an ASPX page using Javascript

I wrote a login form calling a WebMethod for authentication. 我写了一个登录表单,调用WebMethod进行身份验证。 This part is working fine. 这部分工作正常。

If the user has been authenticated, I want to transfer control to another form using javascript. 如果用户已经过身份验证,我想使用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/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. 发生此错误是因为接收回发的页面正在尝试使用Viewstate数据填充其属性的页面,但它无效,因为viewstate是由另一个页面创建的。

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. 这将使您自己处理表单post变量,而不是通过viewstate数据填充页面变量,就像在asp.net回发中一样。

EDIT: 编辑:

The viewstate is a serializable data that represents your page state before it is rendered. viewstate是一个可序列化的数据,表示在呈现之前的页面状态。 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. Viewstate在其他一些内部asp.net架构中是一个糟糕的黑客。 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. 你应该小心viewstate,因为如果你在asp.net页面类中设置了很多属性,就像地狱一样增加了开销。

The problem is the __ViewState field. 问题是__ViewState字段。 ASP.NET tries to validate the __ViewState contents and when you change the destination on the client you confuse it. ASP.NET尝试验证__ViewState内容,当您更改客户端上的目标时,您会混淆它。 This page describes what to do in detail: http://www.codeproject.com/KB/aspnet/jsnopostback.aspx 该页面详细描述了该做什么: http//www.codeproject.com/KB/aspnet/jsnopostback.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM