asp.Netページ間転送方法のまとめ

5052 ワード

1、フォームの提出、






.
form1.submit();
....
この方はASPにあります.NETではASPのため無効です.NETのフォームは常に自分のページに提出され、他のページに提出する場合は特別な処理が必要です.
2、 リンクアドレス転送
受信ページ:
string str = Request["param1"]
3、Session共有
送信ページ:
Session("param1") = "1111";
受信ページ
string str = Session("param1").ToString();
4、アプリケーション共有
送信ページ:
Application("param1") = "oec2003";
受信ページ:
string str = Application("param1").ToString();
この方法は、Applicationが1つのアプリケーションドメイン範囲で共有されているため、すべてのユーザが値を変更および設定できるため、カウンタなどグローバル変数が必要な場所のみを適用します.
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"];
二、もし2つのページの間に大量のパラメータを伝達する必要があるならば、例えばデータクエリーなどのページの時、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;
///
///
///

public string StaDate
{
get{ return this.txtStaDate.Text;}
set{this.txtStaDate.Text = value;}
}
///
///
///

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( "
EndDate:" );
Response.Write( queryPage.EndDate );
}
}
三、多くのクエリーページが結果ページの設定方法を共有している場合:
この方式で重要なのは、「QueryPage queryPage=(QueryPage)Context.Handler;」の変換は、変換が特定のページに依存しない場合にのみ実現されます.
すべてのクエリーページにインタフェースを継承させ、そのインタフェースにメソッドを定義すると、結果ページに構築結果を取得するために必要なパラメータを取得するだけで、複数ページで結果ページを共有する操作が可能になります.
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
{
///
///
///

QueryParams Parameters{get;}
}
3、クエリーページはIQueryParamsインタフェース(QueryPage.aspx)を継承する:


///
/// ,
///

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;
///
///
///

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( "
EndDate:" );
Response.Write( queryParams.EndDate );
}
}