一、目前在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¶m2=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¶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是否存在,極其煩瑣,我想應該還有更簡便的方法來實現頁面間的參數傳遞,故上網查找,終于找到這樣一種方式來實現頁面間的參數傳遞。
? 有不到之處,請大家指正!
我說詳細一點,現在有這樣一種Web開發模式:
Page - 窗體容器,從.net framework接受客戶端請求,并最終向客戶端輸出html,承載UserControl容器并組裝之
UserControl - 功能模塊,對一個特定功能進行封裝,承載WebControl的容器
WebControl - 封裝了單一的業務邏輯,和與之對應對應的HTML輸出、客戶端交互UI
所有的請求參數由Page得到,最終傳遞至UserControl容納的WebControl內
在這個過程中,Page(甚至是UserControl)不提供任何邏輯進行干預,UserControl/WebControl的可變特性完全由持久化的HTML代碼給出
——也就是說,所有的Page都沒有代碼,如何在一個系統中做到Transfer所需要的參數便于維護、調整?
頁面1:
public class Page1 : System.Web.UI.Page
{
public UserControl1 userControl1;
public UserControl2 userControl2;
}
用戶控件1:
public class UserControl2 :System.Web.UI.UserControl
{
private void btnEnter_Click(object sender, System.EventArgs e)
{
Server.Transfer("Page2.aspx");
}
}
//---------------------------------
頁面2:
public class Page2 : System.Web.UI.Page
{
public UserControl3 userControl3;
public UserControl4 userControl4;
}
用戶控件3:
public class UserControl3 :System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
Page1 page1 = (Page1)Context.Handler;
Response.Write( "屬性1" );
Response.Write( page1.userControl1.屬性1 );
Response.Write( "<br/>屬性2" );
Response.Write( page1.userControl2.屬性2 );
}
}
//如果在頁面1中沒有聲明用戶控件1,在頁面2中將引用不到頁面1中用戶控件1中的公共方法
另一種方法是
SourcePage設置一個
??? public string Name
??? {
??????? get { return txtName.Text; }
??? }
目標頁面首先聲明:
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
然后在后臺代碼直接使用
Response.Write(PreviousPage.Name);
posted on 2007-02-09 08:58
SIMONE 閱讀(1155)
評論(0) 編輯 收藏 所屬分類:
.NET