.NET 頁面間傳值的幾種方法


1. QueryString瀏覽器

  這是最簡單的傳值方式,但缺點是傳的值會顯示在瀏覽器的地址欄中且不能傳遞對象,只適用於傳遞簡單的且安全性要求不高的數值。安全

  傳遞:  location.href="WebForm2.aspx?name=" + yourName ;服務器

  接收:  string name = Request.QueryString["name"];cookie

 

2. Formsession

  傳遞:  根據表單內控件的name和value傳遞,如:多線程

       <form id="form1" method="post">post

          <input type="text" name="name" value="xxx" />線程

       </form>orm

       表單須要被提交<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 );

相關文章
相關標籤/搜索