在ASP.NET中页面传值的几种方式(转:具体地址记不清了)

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

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

ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic class QueryPage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
protected System.Web.UI.WebControls.TextBox txtStaDate;
InBlock.gif
protected System.Web.UI.WebControls.TextBox txtEndDate;
InBlock.gifdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 开始时间
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic string StaDate
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gifreturn this.txtStaDate.Text;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{this.txtStaDate.Text = value;}
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 结束时间
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic string EndDate
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gifreturn this.txtEndDate.Text;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{this.txtEndDate.Text = value;}
ExpandedSubBlockEnd.gif}

InBlock.gifdot.gif.
InBlock.gif
private void btnEnter_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifServer.Transfer(
"ResultPage.aspx");
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif在显示查询结果页面(ResultPage.aspx):
None.gif
public class ResultPage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
//转换一下即可获得前一页面中输入的数据
InBlock.gif
QueryPage queryPage = ( QueryPage )Context.Handler;
InBlock.gif
InBlock.gifResponse.Write( 
"StaDate:" );
InBlock.gifResponse.Write( queryPage.StaDate );
InBlock.gifResponse.Write( 
"
InBlock.gif
EndDate:" );
InBlock.gif
Response.Write( queryPage.EndDate );
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif

三、如果有许多查询页面共用一个结果页面的设置方法:
在这种方式中关键在于“ QueryPage queryPage = ( QueryPage )Context.Handler; ”的转换,只有转换不依赖于特定的页面时即可实现。
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作!

1、先定义一个类,用该类放置所有查询参数:

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//// 
InBlock.gif
/// 结果页面中要用到的值
ExpandedBlockEnd.gif
/// 

None.gifpublic class QueryParams
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private string staDate;
InBlock.gif
private string endDate;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 开始时间
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic string StaDate
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gifreturn this.staDate;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{this.staDate = value;}
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 结束时间
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic string EndDate
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gifreturn this.endDate;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{this.endDate = value;}
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif

2、接口定义:

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//// 
InBlock.gif
/// 定义查询接口。
ExpandedBlockEnd.gif
/// 

None.gifpublic interface IQueryParams
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 参数
ExpandedSubBlockEnd.gif
/// 

ExpandedSubBlockStart.gifContractedSubBlock.gifQueryParams Parametersdot.gif{get;}
ExpandedBlockEnd.gif}

None.gif

3、查询页面继承IQueryParams接口(QueryPage.aspx):

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//// 
InBlock.gif
///查询页面,继承接口
ExpandedBlockEnd.gif
/// 

None.gifpublic class QueryPage : System.Web.UI.Page, IQueryParams
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
protected System.Web.UI.WebControls.TextBox txtStaDate;
InBlock.gif
protected System.Web.UI.WebControls.TextBox txtEndDate;
InBlock.gif
InBlock.gif
private QueryParams queryParams;
InBlock.gifdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// 结果页面用到的参数
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic QueryParams Parameters
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return queryParams;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifdot.gif.
InBlock.gif
private void btnEnter_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//赋值
InBlock.gif
queryParams = new QueryParams();
InBlock.gifqueryParams.StaDate 
= this.txtStaDate.Text;
InBlock.gifqueryParams.EndDate 
= this.txtEndDate.Text
InBlock.gif
InBlock.gifServer.Transfer(
"ResultPage.aspx");
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif

4、别外的页面也如此设置
5、接收页面(ResultPage.aspx):

ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic class ResultPage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifQueryParams queryParams 
= new QueryParams();
InBlock.gifIQueryParams queryInterface;
InBlock.gif
//实现该接口的页面
InBlock.gif
if( Context.Handler is IQueryParams)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifqueryInterface 
= ( IQueryParams )Context.Handler;
InBlock.gifqueryParams 
= queryInterface.Parameters;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifResponse.Write( 
"StaDate:" );
InBlock.gifResponse.Write( queryParams.StaDate );
InBlock.gifResponse.Write( 
"
InBlock.gif
EndDate:" );
InBlock.gif
Response.Write( queryParams.EndDate );
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

三、本文起因:
因在工作中要作一个数据查询,参数烦多,原先是用Session传递,用完该Session传来的参数后,还需清理Session,在用Session之前还得判断该Session是否存在,极其烦琐,我想应该还有更简便的方法来实现页面间的参数传递,故上网查找,终于找到这样一种方式来实现页面间的参数传递。
有不到之处,请大家指正!

转载于:https://www.cnblogs.com/erichzhou/archive/2007/03/29/693140.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值