Home  >  Article  >  Web Front-end  >  页面之间传值的方式_html/css_WEB-ITnose

页面之间传值的方式_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:51:271354browse

        在开发web项目的时候难免会遇到页面之间的传值问题,不知你是否知道怎么传值,我所知道的就几种:QueryString,Session,Application,Cookie。但是具体实现该如何:请看下面一一详解:

1.使用QueryString方式

        这种方式,我们可以将浏览器中地址栏中的一些参数给取出来。比如说:           string str=Request.QueryString["username"].toString();

2.使用Session方式

    Session用于保存每个用户的专用信息.她的生存期是用户持续请求时间再加上一段时间(默认是20分钟)

        1)在事件1中将内容保存到session中

             Session["name"] = “hello”;

       2) 在另一页面的事件中取出保存的name变量值

             string name = Session["name"].ToString(); 

3.使用Application 方式

        Application变量在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所以可以在不同页面中对它进行存取。它和Session变量的区别在于,前者是所有的用户共用的全局变量,后者是各个用户独有的全局变量。

         1)在事件1中将内容保存到Application中

             Application["name"] = “hello”;

         2) 在另一页面的事件中取出保存的name变量值

             string name = Application["name"].ToString(); 

4.使用Cookie方式

        在网上找了一个小例子,如下:

          a.aspx的C#代码:

1 private void Button1_Click(object sender, System.EventArgs e)

2 {

3      HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");

4      Response.Cookies.Add(objCookie);  

5 }


b.aspx中C#代码:

1 private void Page_Load(object sender, System.EventArgs e)

2 {

3      string myName1Value;

4      myName1Value = Request.Cookies[ "myCookie" ].Value;

5 }


总结:对于页面传值方式暂时就知道这么几种,希望有助于大家理解。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn