您的位置:首页 > 编程语言 > ASP

ASP.NET几种传值方式

2020-07-16 05:00 621 查看

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;

 

           

转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/04/07/1706030.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: