baochunlin 2008-7-7 15:26
ASP.NET页面间的传值的几种方法
[url=http://whatis.ctocio.com.cn/searchwhatis/451/6092451.shtml]ASP.NET[/url] WEB FORMS 给开发者提供了极好的事件驱动开发模式。然而这种简单的应用程序开发模式却给我们带来了一些小问题,举个例子,在传统的ASP应用程序中,你能够通过[url=http://whatis.ctocio.com.cn/searchwhatis/229/6026229.shtml]POST[/url]方法很容易的把一个值或多个值从一个页面传送到另一个页面,用同样的方法在ASP.NET中实现有点麻烦。在这里,我们可以通过其他方式来解决这种情形。ASP.NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过[url=http://whatis.ctocio.com.cn/searchwhatis/18/5949018.shtml]session[/url]变量来传送相应的值,还有就是通过Server.Transfer方法来实现。下面分别一一介绍:
<P> 一、使用Querystring</P>
<P> Querystring是一种非常简单的传值方式,其缺点就是会把要传送的值显示在浏览器的地址栏中,并且在此方法中不能够传递对象。如果你想传递一个<A class=channel_keylink href="http://security.chinaitlab.com/" target=_blank><FONT color=#0000ff>安全</FONT></A>性不是那么太重要或者是一个简单的数值时,用此方法最好不过了。下面通过一个小例子来完成传值工作,步骤如下:</P>
<P> 1、创建一个web form</P>
<P> 2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2</P>
<P> 3、为button按钮创建[url=http://whatis.ctocio.com.cn/searchwhatis/224/5947224.shtml]click[/url]事件 代码如下:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD> private void Button1_Click ([url=http://whatis.ctocio.com.cn/searchwhatis/330/6025830.shtml]object[/url] sender, System.EventArgs e)<BR> { [url=http://whatis.ctocio.com.cn/searchwhatis/400/6026400.shtml]string[/url] url;<BR> url="webform2.aspx?name=" +<BR> TextBox1.Text + "&email=" +<BR> TextBox2.Text;<BR> Response.Redirect(url);<BR> }</TD></TR></TBODY></TABLE></P>
<P> 4、新建一个目标页面命名为webform2</P>
<P> 5、在webform2中放置两个Label1,Label2 在webform2的Page_Load中添加如下代码:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD> private void Page_Load (object sender, System.EventArgs e)<BR> {<BR> Label1.Text=Request.QueryString["name"];<BR> Label2.Text=Request.QueryString["email"];<BR> }</TD></TR></TBODY></TABLE></P>
<P> 运行,即可看到传递后的结果了。</P>
<P> 二、使用Session变量</P>
<P> 使用Session变量传值是一种最常见的方式了,此中方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Session变量的值removed后,变量才会消失。举个例子看看:</P>
<P> 1、创建一个web form</P>
<P> 2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2</P>
<P> 3、为button按钮创建click事件 代码如下:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD> private void Button1_Click (object sender, System.EventArgs e)<BR> {<BR> Session["name"]=TextBox1.Text;<BR> Session["email"]=TextBox2.Text;<BR> Response.Redirect("webform2.aspx");<BR> }</TD></TR></TBODY></TABLE></P>4、新建一个目标页面命名为webform2
<P> 5、在webform2中放置两个Label1,Label2 在webform2的Page_Load中添加如下代码:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD> private void Page_Load (object sender, System.EventArgs e)<BR> {<BR> Label1.Text=Session["name"].ToString();<BR> Label2.Text=Session["email"].ToString();<BR> Session.Remove("name");<BR> Session.Remove("email");<BR> }</TD></TR></TBODY></TABLE></P>
<P> 运行,即可看到传递后的结果了。</P>
<P> 三、使用Server.Transfer</P>
<P> 虽然这种方法有点复杂,但也不失为一种在页面传值的方式。 举个例子看看:</P>
<P> 1、创建一个web form</P>
<P> 2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2</P>
<P> 3、为button按钮创建click事件 代码如下:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD>
<P> private void Button1_Click (object sender, System.EventArgs e)<BR> {<BR> Server.Transfer("webform2.aspx");<BR> }</P></TD></TR></TBODY></TABLE></P>
<P> 4、创建过程来返回TextBox1,TextBox2控件的值代码如下:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD>
<P> public string Name<BR> {<BR> get<BR> {<BR> return TextBox1.Text;<BR> }<BR> }</P>
<P> public string EMail<BR> {<BR> get<BR> {<BR> return TextBox2.Text;<BR> }<BR> }</P></TD></TR></TBODY></TABLE></P>
<P> 5、新建一个目标页面命名为webform2</P>
<P> 6、在webform2中放置两个Label1,Label2 在webform2的Page_Load中添加如下代码:
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=1 width="80%" align=center bgColor=#ffffff border=1 heihgt="">
<TBODY>
<TR>
<TD> private void Page_Load (object sender, System.EventArgs e)<BR> {<BR> //创建原始窗体的实例<BR> WebForm1 wf1;<BR> //获得实例化的句柄<BR> wf1=(WebForm1)Context.Handler;<BR> Label1.Text=wf1.Name;<BR> Label2.Text=wf1.EMail;<BR> }</TD></TR></TBODY></TABLE></P>
<P> 运行,即可看到传递后的结果了。</P>
dongxixiang68bc 2008-7-11 13:18
初学.net又涨知识了!
wkdahliab4e4be 2008-7-15 17:56
非常好,谢谢了~
shimmer84301e20 2008-12-26 11:13
学习了
michaelxue515b0 2008-12-26 23:22
我正想要的十分感谢!!!