先来建两个测试页面:test1.aspx和test2.aspx,内容如下:
test1.aspx 页面:
<%@ Page language="c#" Codebehind="Test1.aspx.cs" AutoEventWireup="false" Inherits="Ctrls.Test1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<!-- 下面在 from 标签中加了onsubmit事件处理,以便在提交数据时指向其他的页面 -->
<form id="Form1" method="post" runat="server" onsubmit="this.action='test2.aspx'">
请输入您的姓名:<asp:TextBox id=txtName runat="server"></asp:TextBox>
<asp:Button id=btnOK runat="server" Text="发送" Width="56px"></asp:Button>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<!-- 下面在 from 标签中加了onsubmit事件处理,以便在提交数据时指向其他的页面 -->
<form id="Form1" method="post" runat="server" onsubmit="this.action='test2.aspx'">
请输入您的姓名:<asp:TextBox id=txtName runat="server"></asp:TextBox>
<asp:Button id=btnOK runat="server" Text="发送" Width="56px"></asp:Button>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ctrls
{
/// <summary>
/// Test1 的摘要说明。
/// </summary>
public class Test1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.Button btnOK;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ctrls
{
/// <summary>
/// Test1 的摘要说明。
/// </summary>
public class Test1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.Button btnOK;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
}
}
test2.aspx 文件内容:
<%@ Page language="c#" Codebehind="Test2.aspx.cs" AutoEventWireup="false" Inherits="Ctrls.Test2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server"><FONT face=宋体>您的姓名:<asp:Label id=lbName runat="server"></asp:Label><BR><BR>
你的地址:<asp:TextBox id=txtAddress runat="server"></asp:TextBox><asp:Button id=btnOK runat="server" Text="发送" Width="64px"></asp:Button><BR><asp:Label id=lbAddr runat="server"></asp:Label></FONT>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server"><FONT face=宋体>您的姓名:<asp:Label id=lbName runat="server"></asp:Label><BR><BR>
你的地址:<asp:TextBox id=txtAddress runat="server"></asp:TextBox><asp:Button id=btnOK runat="server" Text="发送" Width="64px"></asp:Button><BR><asp:Label id=lbAddr runat="server"></asp:Label></FONT>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ctrls
{
/// <summary>
/// Test2 的摘要说明。
/// </summary>
public class Test2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtAddress;
protected System.Web.UI.WebControls.Button btnOK;
protected System.Web.UI.WebControls.Label lbAddr;
protected System.Web.UI.WebControls.Label lbName;
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
lbName.Text = Convert.ToString(Request.Form["txtName"]);
}
}
Web 窗体设计器生成的代码
private void btnOK_Click(object sender, System.EventArgs e)
{
lbAddr.Text = txtAddress.Text;
}
}
}
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ctrls
{
/// <summary>
/// Test2 的摘要说明。
/// </summary>
public class Test2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtAddress;
protected System.Web.UI.WebControls.Button btnOK;
protected System.Web.UI.WebControls.Label lbAddr;
protected System.Web.UI.WebControls.Label lbName;
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
lbName.Text = Convert.ToString(Request.Form["txtName"]);
}
}
Web 窗体设计器生成的代码
private void btnOK_Click(object sender, System.EventArgs e)
{
lbAddr.Text = txtAddress.Text;
}
}
}
上面两个页面与平常的页面一样,只是在test1.aspx页面中,设置了form标签的onsubmit事件,以使用在发送数据之前,把页面指向其他的页面:<form id="Form1" method="post" runat="server" onsubmit="this.action='test2.aspx'">.
打开test1.aspx页面,填写数据之后按提交按钮,页面将数据提交到test2.aspx页面,但是这时我们将得到一个"此页的视图状态无效,可能已损坏。"的运行时错误.
为了解决这个问题,我们可以重写Page类的DeterminePostBackMode方法.修改test2.aspx.cs文件,加入DeterminePostBackMode方法,如下:
/// <summary>
/// 此方法用来获取表单的数据,如果此方法返回null值的话, IsPostBack 属性将会设置为 false
/// 当 IsPostBack 属性为 false 时,ASP.NET就不会加载视图状态的逻辑,那也就不会出
/// 现"此页的视图状态无效,可能已损坏。"的运行时错误了
/// </summary>
protected override NameValueCollection DeterminePostBackMode()
{
// 这里主要检查是否是从其他页面请求过来的,
// 如果是从其他页面请过来就返回空,不加载视图状态
if (Request.UrlReferrer == null)
{
return null;
}
string url = Request.UrlReferrer.ToString();
url = url.Substring(url.LastIndexOf("/") + 1).ToLower();
if (url != "test2.aspx")
{
return null;
}
return base.DeterminePostBackMode ();
}
/// 此方法用来获取表单的数据,如果此方法返回null值的话, IsPostBack 属性将会设置为 false
/// 当 IsPostBack 属性为 false 时,ASP.NET就不会加载视图状态的逻辑,那也就不会出
/// 现"此页的视图状态无效,可能已损坏。"的运行时错误了
/// </summary>
protected override NameValueCollection DeterminePostBackMode()
{
// 这里主要检查是否是从其他页面请求过来的,
// 如果是从其他页面请过来就返回空,不加载视图状态
if (Request.UrlReferrer == null)
{
return null;
}
string url = Request.UrlReferrer.ToString();
url = url.Substring(url.LastIndexOf("/") + 1).ToLower();
if (url != "test2.aspx")
{
return null;
}
return base.DeterminePostBackMode ();
}
在重新打开test1.aspx,然后把数据提交到test2.aspx页面,这样就不会出现错误了,页且在test2.aspx页面还可以做回传处理呢.
文章出处:我的网站