<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"]
{
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");
}
}
{
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 );
}
}
/// 結果頁面中要用到的值
/// </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;}
}
}
/// 定義查詢接口。
/// </summary>
public interface IQueryParams
{
/// <summary>
/// 參數
/// </summary>
QueryParams Parameters{get;}
}
///查詢頁面,繼承接口
/// </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");
}
}
{
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 );
}
}