ASP.NET十分有用的页面间传值方法

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

一、目前在ASP.NET中页面传值共有这么几种方式:
1、表单提交
  <formaction="target.ASPx"method="post"name="form1">
 <inputname="param1"value="1111"/>
 <inputname="param2"value="2222"/>
  </form>
  ....
  form1.submit();
  ....
  此种方在ASPNET中无效,因为ASPNET的表单总是提交到自身页面,如果要提交到别一页面,需要特殊处理。
2、链接地址传送
接收页面:stringstr=Request["param1"]
3、Session共享
发送页面:Session("param1")="1111"; 
按收页面 stringstr=Session("param1").ToString(); 
4、Application共享
发送页面:Application("param1")="1111";  
按收页面:stringstr=Application("param1").ToString(); 
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
5、Cookie
6、Response.Redirect()方式
  Response.Redirect("target.ASPx?param1=1111¶m2=2222")
  接收页面:stringstr=Request["param1"]
7、Server.Transfer()方式。
  Server.Transfer("target.ASPx?param1=1111¶m2=2222")
  接收页面:stringstr=Request["param1"]

二、如果在两个页面间需要大量的参数要传传递,如数据查询等页面时,用1-6的方法传值及其不便,而第7种方法确有一独特的优势!但使用该方法时需要一定的设置,现简单介绍一下该方法的使用方式:
  以查询数据页面为例:
  在查询页面中设置如下公有属性(QueryPage.ASPx): Www.


publicclassQueryPage:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.TextBoxtxtStaDate;
protectedSystem.Web.UI.WebControls.TextBoxtxtEndDate;
  ...
///<summary>
///开始时间
///</summary>
publicstringStaDate
{
get{returnthis.txtStaDate.Text;}
set{this.txtStaDate.Text=value;}
}
///<summary>
///结束时间
///</summary>
publicstringEndDate
{
get{returnthis.txtEndDate.Text;}
set{this.txtEndDate.Text=value;}
}
....
privatevoidbtnEnter_Click(objectsender,System.EventArgse)
{
Server.Transfer("ResultPage.ASPx");
}
}  _com

在显示查询结果页面(ResultPage.ASPx):


 publicclassResultPage:System.Web.UI.Page
{
  privatevoidPage_Load(objectsender,System.EventArgse)
  {

//转换一下即可获得前一页面中输入的数据
QueryPagequeryPage=(QueryPage)Context.Handler;

Response.Write("StaDate:");
Response.Write(queryPage.StaDate);
Response.Write("<br/>EndDate:");
Response.Write(queryPage.EndDate);
 }
}  ^com

Www@@com

共3页:上一页1

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值