一、目前在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):

以下为引用的内容:
public class QueryPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtStaDate;
protected System.Web.UI.WebControls.TextBox txtEndDate;
   ...
/// <summary>
/// 开始时间
/// </summary>
public string StaDate
{
get{ return this.txtStaDate.Text;}
set{this.txtStaDate.Text = value;}
}
/// <summary>
/// 结束时间
/// </summary>
public string EndDate
{
get{ return this.txtEndDate.Text;}
set{this.txtEndDate.Text = value;}
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
Server.Transfer("ResultPage.aspx");
}

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

以下为引用的内容:
 public class ResultPage : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {

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

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

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

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

以下为引用的内容:
/// <summary>
/// 结果页面中要用到的值
/// </summary>
public class QueryParams
{
private string staDate;
private string endDate;

/// <summary>
/// 开始时间
/// </summary>
public string StaDate
{
get{ return this.staDate;}
set{this.staDate = value;}
}
/// <summary>
/// 结束时间
/// </summary>
public string EndDate
{
get{ return this.endDate;}
set{this.endDate = value;}
}
}

2、接口定义:

以下为引用的内容:

/// <summary>
/// 定义查询接口。
/// </summary>
public interface IQueryParams
{
/// <summary>
/// 参数
/// </summary>
QueryParams Parameters{get;}
}

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

以下为引用的内容:

/// <summary>
///查询页面,继承接口
/// </summary>
public class QueryPage : System.Web.UI.Page, IQueryParams
{
protected System.Web.UI.WebControls.TextBox txtStaDate;
protected System.Web.UI.WebControls.TextBox txtEndDate;
private QueryParams queryParams;
   ...
/// <summary>
/// 结果页面用到的参数
/// </summary>
   public QueryParams Parameters
{
get
{
return queryParams;
}
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
//赋值
queryParams = new QueryParams();
queryParams.StaDate = this.txtStaDate.Text;
queryParams.EndDate = this.txtEndDate.Text

Server.Transfer("ResultPage.aspx");
}
}

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

以下为引用的内容:
public class ResultPage : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {

QueryParams queryParams = new QueryParams();
IQueryParams queryInterface;
//实现该接口的页面
if( Context.Handler is IQueryParams)
{
queryInterface = ( IQueryParams )Context.Handler;
queryParams = queryInterface.Parameters;
}

Response.Write( "StaDate:" );
Response.Write( queryParams.StaDate );
Response.Write( "<br/>EndDate:" );
Response.Write( queryParams.EndDate );
  }
}

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

转载于:https://www.cnblogs.com/cuihongyu3503319/archive/2008/07/31/1257101.html

ASP.NET十分有用的页面间传值方法(转)相关推荐

  1. java 页面之间传值_JSP页面间传值方法

    JSP页面间传值方法[@more@] a:最常用的方法是用form中的text, ,然后在b.jsp页面中这样获取 String username=request.getParameter(" ...

  2. asp.net WebForm页面间传值方法

    ASP.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式.然而这种简单的应用程序开发模式却给我们带来了一些小问题,举个例子,在传统的ASP应用程序中,你能够通过POST方法很容易的把一 ...

  3. WebForm页面间传值方法(转)

    Asp.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式.Asp .NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过session变量来传 ...

  4. 微信小程序页面间传值方法

    1 通过data-*属性传值 console.log(e.currentTarget.dataset) 实例:data- 是固定的写法,-后面的名字可以随便起字母或英文,但获取的时候要写对应名称. v ...

  5. 【项目经验】——ASP.NET页面间传值

    最近项目中涉及到很多页面传值的内容,今天,就和大家分享一下最常用的集中ASP.NET页面间传值的几种方法. 一.QueryString传值 QueryString是平常传值中用的比较多的一种方式,使用 ...

  6. Windows Phone 7 - 页面间传值 来源-http://blog.csdn.net/dncts/article/details/6160067

    Windows Phone 7 - 页面间传值 分类: Windows Phone 7 2011-01-23 20:42 441人阅读 评论(0) 收藏 举报 在页面间导航时传递参数的方式大致有3种, ...

  7. Asp.net页面间传值方式汇总

    在优化.NET中,用到了页面传值的功能,汇总出来跟大家分享一下. 我找到了七种传值方式,分别是:URL传值,Session传值,Cookie传值,Server.Transfer传值,Applicati ...

  8. HTML框架IFrame结合JS在主页面和子页面间传值

    下面主页面和子页面互相传值的DEMO 如果仅仅需要子页面触发主页面的函数 仅需 [ parent.window.你的函数 ] 就可以了 DOM方法: 父窗口操作IFRAME:window.frames ...

  9. html传值方式有哪几种,关于html页面间传值的几种方法

    问题 因最近尝试实现客户端与服务端分离,服务端只提供接口,客户端用html+js实现,分成两个独立的项目部署,因项目是个人项目,客户端展示不考虑使用像Angular.Vue.Native这种前端框架实 ...

最新文章

  1. 1068 Find More Coins (30 分)【难度: 难 / 知识点:01背包问题 + 找路径】
  2. 【通俗解释】余弦相似度
  3. java中static类的作用是什么意思_java中static关键字是什么意思
  4. 想问问你们都是什么时候考驾照的?
  5. 电商之争:亚马逊与阿里一较高下
  6. 百度云下载的压缩吧损坏问题解决
  7. 选拔人才的原则和误区
  8. 如何看计算机系统是x86,怎么看电脑是x86还是x64 x64和x86有区别讲解分享
  9. 介词 by during for from
  10. Android面试题目及其答案
  11. c语言循环题兔子第三个月生,C语言·古典问题: 兔子总数
  12. 2011.4.5 凌晨 3:50分
  13. 【干货】初中数学思维导图
  14. IT行业工作的就业方向
  15. 阿里云apt-get安装包时Err:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 git amd64
  16. jQuery中的Ajax (六个Ajax的操作方法) 细解!!!
  17. 【论文阅读】Don‘t be so sure! Boosting ASR Decoding via Confidence Relaxation
  18. 《电路分析基础》第2章 电阻电路的基本分析方法 读书笔记
  19. 做好这三点,让家装少点后悔事
  20. 宁波工程学院 OJ [1347] 老虎满坡找灰兔

热门文章

  1. WIN7 64位系统注册银行支付组件
  2. VALVE SURVEY RESULTS
  3. SQL 语法参考手册
  4. Ubuntu 14.10安装libvirt KVM
  5. NYOJ_269_VF
  6. 解决 Flex navigateToURL 中文乱码问题
  7. 【做事必须搞清10个顺序】
  8. WebKit DOM Event (二)
  9. WebCore中的渲染机制(二):块和内嵌(Blocks and Inlines)
  10. Linux中硬盘转速查看