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、<A href="target.aspx?param1=1111&param2=2222">链接地址传送</A>
接收页面: 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&param2=2222")
   接收页面: string str = Request["param1"]
7、Server.Transfer()方式。
   Server.Transfer("target.aspx?param1=1111&param2=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();
I      QueryParams 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 );
  }
}

 

具体解释如下:

使用QueryString变量
 QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。下面是一个例子:
a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

string s_url;

s_url = "b.aspx?name=" + Label1.Text;

Response.Redirect(s_url);

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

Label2.Text = Request.QueryString["name"];

}

使用Application 对象变量
Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。

a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

Application["name"] = Label1.Text;

Server.Transfer("b.aspx");

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

string name;

Application.Lock();

name = Application["name"].ToString();

Application.UnLock();

}

使用Session变量
想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人而且用完该Session传来的参数后,还需清理Session,在用Session之前还得判断该Session是否存在,否则,过量的存储Session不销毁最终占用服务器内存资源。
a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

Session["name"] = Label.Text;

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

string name;

name = Session["name"].ToString();

}

使用Cookie对象变量
这个也是大家常使用的方法,与Session一样,不过是针对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用

a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)

{

HttpCookie cookie_name = new HttpCookie("name");

cookie_name.Value = Label1.Text;

Reponse.AppendCookie(cookie_name);

Server.Transfer("b.aspx");

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

string name;

name = Request.Cookie["name"].Value.ToString();

}

使用Server.Transfer方法
这个才可以说是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。
1)Server.Transferk可以直接用Request ,实际上属于Request.QueryString["param1"]类
    Server.Transfer("target.aspx?param1=1111&param2=2222")
    接收页面: string str = Request["param1"]

2)a.aspx的C#代码

public string Name

{

get{ return Label1.Text;}

}

private void Button1_Click(object sender, System.EventArgs e)

{

Server.Transfer("b.aspx");

}

b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)

{

a newWeb; //实例a窗体
//newWeb = (a)Context.Handler;或者直接用下面的
newWeb = (source)Context.Handler;

string name;

name = newWeb.Name;

}
延伸一下:如果有许多查询页面共用一个结果页面的又怎么设置。
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作!
   1)先定义一个类,用该类放置所有查询参数。
    public class QueryParams
{
private string staDate;
private string endDate;
public string StaDate
{
get{ return this.staDate;}
set{this.staDate = value;}
}
public string EndDate
{
get{ return this.endDate;}
set{this.endDate = value;}
}
}
   2)接口定义
   public interface IQueryParams
  {
     /// <summary>
    /// 参数
   /// </summary> 
   QueryParams Parameters{get;}
  }

a.aspx的C#代码,页面继承IQueryParams接口
/// <summary>
///继承接口
/// </summary>
public class a: System.Web.UI.Page, IQueryParams
{
protected TextBox txtStaDate;
protected TextBox txtEndDate;

private QueryParams queryParams;
   ...
   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("b.aspx");
}
}
b.aspx人C#代码,接收页面.
public class b: 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是否存在,极其烦琐,我想应该还有更简便的方法来实现页面间的参数传递,故上网查找,终于找到这样一种方式来实现页面间的参数传递。
  有不到之处,请大家指正!

posted @ 2014-03-05 13:10  mingli  阅读(193)  评论(0编辑  收藏  举报