kuku
笔记
博客园
首页
新随笔
联系
订阅
管理
随笔-27 评论-17 文章-1 trackbacks-4
在ASP.NET中把数据POST到其他页面
先来建两个测试页面: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
>
test1.aspx.cs 文件内容:
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 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
}
}
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
>
test2.aspx.cs 文件内容:
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 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.btnOK.Click
+=
new
System.EventHandler(
this
.btnOK_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
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 ();
}
在重新打开test1.aspx,然后把数据提交到test2.aspx页面,这样就不会出现错误了,页且在test2.aspx页面还可以做回传处理呢.
posted on 2004-11-08 11:23
匡匡
阅读(1902)
评论(2)
编辑
收藏
所属分类:
ASP.NET
评论:
#1楼
2004-11-08 11:58 |
squirrel_sc
可以在test1.aspx里面用Server.Transfer("test2.aspx", true),这样可能简单一些。在2003server上,可以postback,xp的iis5.1下不行,2000下好像也能postback。
回复
引用
查看
#2楼
2004-11-11 09:06 |
ben
楼上说得对,这个操作有问题,如果页面中不只是TextBox Control,存在其它Control(例如ListBox、DrapDownList...)在test2.aspx可能会接收不到。
回复
引用
查看
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2004-11-30 09:17 编辑过
相关文章:
ASP.NET AJAX入门系列
【翻译】Scott Mitchell的ASP.NET2.0数据指南中文版索引
.NET设计模式系列文章
ASP.NET 大文件上传组件[无组件上传](AspnetUpload 2.3 release)[免费版序列号放送中...]
prototype.js 1.4版开发者手册(强烈推荐)
ASP.NET AJAX入门系列(2):使用ScriptManager控件
相关链接:
所属分类的其他文章:
完全理解 IDisposable 接口的实现
GridView 几个受保护的方法的注释
在ASP.NET去掉文件的只读属性
.NET中三种数据类型转换的区别:(type), type.Parse, Convert类
在ASP.NET中把数据POST到其他页面
最新IT新闻:
马云vs孙正义:两个“疯子”的对话
消息称MySQL创始人已向Sun提交辞呈
谷歌Chrome浏览器即将更换LOGO颜色?
淘宝网合并阿里妈妈 专家称阿里巴巴或有新战略
微软研究院发布 AutoCollage - 整理并融合照片
博客园新闻频道
博客园首页
社区
<
2004年11月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(5)
给我留言
查看留言
我参加的小组
Visual Studio
AJAX
.NET 3.x
我参与的团队
珠海.NET俱乐部(0/160)
随笔分类
.NET Framework(2)
ASP.NET(5)
Java(3)
JavaScript(3)
SQL Server(2)
WPF(6)
其他(3)
随笔档案
2008年5月 (1)
2008年4月 (4)
2007年12月 (1)
2007年11月 (4)
2007年6月 (3)
2007年2月 (5)
2007年1月 (5)
2004年12月 (2)
2004年11月 (2)
积分与排名
积分 - 14120
排名 - 2602
最新评论
阅读排行榜
1. 在网页中使用javascript打开没有最大化、最小化和关闭按钮的窗口(4835)
2. 在ASP.NET去掉文件的只读属性(2138)
3. 在ASP.NET中把数据POST到其他页面(1902)
4. .NET中三种数据类型转换的区别:(type), type.Parse, Convert类(1229)
5. 完全理解 IDisposable 接口的实现(359)
评论排行榜
1. 在网页中使用javascript打开没有最大化、最小化和关闭按钮的窗口(8)
2. 在ASP.NET去掉文件的只读属性(3)
3. 在ASP.NET中把数据POST到其他页面(2)
4. WPF 中的 BitmapEffect(1)
5. .NET中三种数据类型转换的区别:(type), type.Parse, Convert类(1)