asp.net 頁面間傳值方法小結

來源:互聯網
上載者:User

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&m2=2222">連結地址傳送</A>
接收頁面:
string str = Request["param1"]
3、Session共用
發送頁面:
Session("param1") = "1111";
按收頁面
string str = Session("param1").ToString();
4、Application共用
發送頁面:
Application("param1") = "oec2003";
按收頁面:
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 );
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.