標籤:http io os 使用 sp for on 資料 bs
1. QueryString
這是最簡單的傳值方式,但缺點是傳的值會顯示在瀏覽器的地址欄中且不能傳遞對象,只適用於傳遞簡單的且安全性要求不高的數值。
傳遞: location.href="WebForm2.aspx?name=" + yourName&&name2="+ yourName2;
接收: string name = Request.QueryString["name"];
2. Form
傳遞: 根據表單內控制項的name和value傳遞,如:
<form id="form1" method="post">
<input type="text" name="name" value="xxx" />
</form>
表單需要被提交<input type="submit" /> 或者 document.getElementById("form1").submit();
接收: string name = Request.Form["name"];
3. Session
需要注意的是在Session變數儲存過多的資料會消耗比較多的伺服器資源,在使用session時應該使用一些清理動作來去除一些不需要的session。
移除: Session.Remove("name");
傳遞: Session["name"] = yourName;
接收: string name=Session["name"].ToString();
4. Cookie
傳遞: HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = yourName;
Response.AppendCookie(cookie_name);
接收: Request.Cookies["name"].Value;
5. Application
Application對象的作用範圍是整個全域,也就是說對所有使用者都有效。其常用的方法用Lock和UnLock。
傳遞: Application["name"] = name;
接收: Application.Lock();
string name = Application["name"].ToString();
Application.UnLock();
//Lock的目的是為了防止被多線程篡改,保證這一時刻只有自己在改
6. Server.Transfer
傳遞:
WebForm1寫好需要被傳值的屬性 如:
public string Name { get{ return txtName.Text; } }
執行Server.Transfer("WebForm2.aspx");
接收:
WebForm2中接收參數:
WebForm1 wf1=(WebForm1)Context.Handler;
Response.Write( wf1.Name );
.NET 頁面間傳值的幾種方法