1、QueryString
特点:值参数显示在地址栏中。
适用于安全性不高和简单的数值,但是长度有限。
例如:
传值方式:Response.Redirect("../Login.aspx?UserName=" + this.txt_UserName.Text.Trim());
取值方式:Request.QueryString["UserName"]
2、Session
特点:存放在服务器端,占用服务器资源。
传值方式:Session["UserName"] = this.txt_UserName.Text.Trim();
取值方式:this.txt_UserName.Text = Session["UserName"] != null ? Session["UserName"].ToString() :"";
3、Cookie
特点:存放在客户端,不占服务器资源,需要配合Request使用。
传值方式:
HttpCookie _cookie = new HttpCookie("UserName");
_cookie.Value = "Tim";
Response.AppendCookie(_cookie);
Server.Transfer("../Login.aspx");
取值方式:
string userName = Request.Cookies["UserName"]!=null?Request.Cookies["UserName"].ToString():"";
4、Application
特点:全局变量,使用时配合Lock和Unlock方法。
传值方式:Application["username"] = "Tim";
取值方式:Application.Lock();string username = Application["username"].ToString();Application.UnLock();
5、Server.Transfer
特点:将当前页面引导到另一个页面,新的页面使用前页面的应答流。
传值方式:
Server.Transfer("Login.aspx");
取值方式:
方法1、TextBox txt = (TextBox)PreviousPage.FindControl("TextBox1");
方法2、当前页面public string username{return this.textbox1.text;};
另一个页面头部引用<%@ Reference Page="~/Login.aspx" %>
页面内
Login webForm = Context.Handler as Login;
string name = webForm._userName;