同一頁面.aspx與.aspx.cs之間參數傳遞
1. .aspx.cs接收.aspx的參數:由於.aspx和.aspx.cs為繼承關系,所以.aspx.cs可以直接對.aspx中的ID進行值提取,具體語句為string b = a.text; 其中a為.aspx中的文本框的ID;
2. .aspx接收.aspx.cs的變量:將.aspx.cs的變量設為全局變量,在.aspx中直接引用<%=a %>,這里a為.aspx.cs中聲明的全局變量;
3.ViewState(頁面級)使用方式: 作用域---頁面級
保存數據方式:
復制代碼代碼如下:
ViewState["myKey"]="MyData";
讀取數據方式:
String myData;
if(ViewState["myData"]!=null)
{
myData = (string)ViewState["myKey"]
}
ViewState不能存儲所有的數據類型,僅支持:
String、Integer、Boolean、Array、ArrayList、Hashtable
不同頁面之間的參數傳遞
1.URL傳遞參數方法,有兩種方法:
第一種:send.aspx
<a href=receive.aspx?a=b></a>
receive.aspx.cs
string c = Request.QueryString["a"];
第二種:send.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
Request.Redirect("receive.aspx?a=b");
}
receive.aspx.cs:
string username = Request.QueryString["username"];
2. Form表單POST方法
send.aspx
<form id="form1" runat="server" action="receive.aspx" method=post>
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="a" runat="server"></asp:TextBox>
</div>
</form>
receive.aspx.cs
string b = Ruquest.Form["a"];
3.通過session方法傳遞參數
send.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = "a";
Request.Redirect("receive.aspx");
}
receive.aspx:
string username = Session["username"];
4.通過cookie方法傳遞參數
HttpCookie keepCookie = new HttpCookie("Login"); //創建一個HttpCookie實例,Cookies名稱為Login,實例只是一個容器,真正使用的是Cookie名稱
keepCookie["userName"] = "www.kpdown.com"; //向Login中添加一個userName屬性,並賦值
keepCookie.Expires = DateTime.Now.AddDays(2); //設定Cookies的有效期為兩天
Response.Cookies.Add(keepCookie); //把Cookies對象返回給客戶端