1.querystring的方式:將需要的值直接寫到連結的後面,這些內容將直接顯示到
地址欄中,在傳遞安全性要求不高的一個或多個值或者是結構簡單的值就可以使
用這樣的方法。
如: Response.Redirect( "target.aspx?param1=hello?m2=hi ")
在接收頁面可以通過request的方式得到所傳遞的值: string str =
Request.QueryString["param1"];
2.cookie方式,使用cookie對象方式,cookie是放在用戶端的
設定Cookie: HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = Label1.Text;
Reponse.AppendCookie(cookie_name);
擷取Cookie:
string name= Request.Cookie
["name"].Value.ToString();
3.session變數,session是放置於伺服器端的
設定Session: Session["name"] ="hello";
擷取Session: string name = Session["name"].ToString();
一般比較提倡用session安全性高,如果頁面需要傳的變數很多的話可以用
hashtable或者數組儲存數值然後存到session當中。
4.使用Application 物件變數
Application對象的作用範圍是整個全域,也就是說對所有使用者都有效。此種方法
不常使用,因為Application在一個應用程式定義域範圍共用,所有使用者可以改變及設
置其值,故只應用計數器等需要全域變數的地方。
設定Application : Application["name"] = ="hello";
擷取Application : string name = Application["name"].ToString();
5. PostBackUrl()方法
default.aspx頁面:
Code
<asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage"
PostBackUrl="~/Default2.aspx" />
default2.aspx頁面:
Code
if (PreviousPage != null)
{
TextBox textBox1 = (TextBox)PreviousPage.FindControl
("TextBox1");
Response.write(textBox1.Text );
}、
6.用Server.Transfer方法
這個才可以說是面象對象開發所使用的方法,其使用Server.Transfer方法把
流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以
這個方法是完全面象對象的,簡潔有效。下面這個代碼是展示在需要很多個參數
的時候,使用的方法,如果參數比較少就沒必要使用這個方法了.
如果讓所有的查詢頁面都繼承一個介面,在該介面中定義一個方法,該方法的唯
一作用就是讓結果頁面獲得構建結果時所需的參數,就可實現多頁面共用一個結果
頁面操作。
1、先定義一個類,用該類放置所有查詢參數:
Code
/**//// <summary>
/// QueryParams 的摘要說明
/// </summary>
public class QueryParams
{
private string firstName;
private string lastname;
private int age;
public string Firstname
{
get { return this.firstname; }
set { this.firstname = value; }
}
public string LastName
{
get { return this.lastname; }
set { this.lastname = value; }
}
public string Age
{
get { return this.age; }
set { this.age = value; }
}
}
2、介面定義:
Code
/**//// <summary >
/// 定義查詢介面。
/// </summary >
public interface IQueryParams
{
/**//// <summary >
/// 參數
/// </summary >
QueryParams Parameters { get;}
}
3、查詢頁面繼承IQueryParams介面(QueryPage.aspx):
QueryPage.aspx
Code
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:Button ID="btnEnter" runat="server" Text="Button"
OnClick="btnEnter_Click" /></div>
</form>
QueryPage.aspx.cs
Code
public partial class QueryPage : System.Web.UI.Page, IQueryParams
{
private QueryParams queryParams;
public QueryParams Parameters
{
get
{
return queryParams;
}
}
public void btnEnter_Click(object sender,
System.EventArgs e)
{
//賦值
queryParams = new QueryParams();
queryParams.FirstnName = this.txtFirstName.Text;
queryParams.Lastname = this.txtLastName.Text;
queryParams.Age = this.txtAge.Text;
Server.Transfer( "ResultPage.aspx ");
}
protected void Page_Load(object sender, EventArgs e)
{ }
}
4、接收頁面(ResultPage.aspx):
ResultPage.aspx.cs
public partial class ResultPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
QueryParams queryParams = new QueryParams();
IQueryParams queryInterface;
//實現該介面的頁面
if (Context.Handler is IQueryParams)
{
queryInterface = (IQueryParams)Context.Handler;
queryParams = queryInterface.Parameters;
}
Response.Write("FirstName: ");
Response.Write(queryParams.FirstName);
Response.Write(" <br/ >Lastname: ");
Response.Write(queryParams.LastName);
Response.Write(" <br/ >Age: ");
Response.Write(queryParams.Age);
}
}
7.其他方式
在接收頁 的html代碼裡加上一行:
<%@ Reference Page = "WebForm1.aspx" %>
WebForm1 fp=(WebForm1)Context.Handler;
this.TextBox1.Text=fp.name; //name 是第一頁的public變數