zoukankan      html  css  js  c++  java
  • 页面传值(转贴)

     今天TM群里有人问起关于页面传值的方法,又引发了一场讨论。看来这个还是有很多人关注的,因此我就我个人观点做了些总结,希望对大家有所帮助。
      1.  使用QueryString变量
        QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。下面是一个例子:
    a.aspx的C#代码
    private void Button1_Click(object sender, System.EventArgs e)
    {
        string s_url;
        s_url = "b.aspx?name=" + Label1.Text;
        Response.Redirect(s_url);
    }

    b.aspx中C#代码
    private void Page_Load(object sender, EventArgs e)
    {
        Label2.Text = Request.QueryString["name"];
    }

      2.  使用Application 对象变量
        Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。
    a.aspx的C#代码
    private void Button1_Click(object sender, System.EventArgs e)
    {
        Application["name"] = Label1.Text;
        Server.Transfer("b.aspx");
    }

    b.aspx中C#代码
    private void Page_Load(object sender, EventArgs e)
    {
        string name;
        Application.Lock();
        name = Application["name"].ToString();
        Application.UnLock();
    }

      3.  使用Session变量
        想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。
    a.aspx的C#代码
    private void Button1_Click(object sender, System.EventArgs e)
    {
        Session["name"] = Label.Text;
    }

    b.aspx中C#代码
    private void Page_Load(object sender, EventArgs e)
    {
        string name;
        name = Session["name"].ToString();
    }

      4.  使用Cookie对象变量
        这个也是大家常使用的方法,与Session一样,其是什对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。

    a.aspx的C#代码
    private void Button1_Click(object sender, System.EventArgs e)
    {
        HttpCookie cookie_name = new HttpCookie("name");
        cookie_name.Value = Label1.Text;
        Reponse.AppendCookie(cookie_name);
        Server.Transfer("b.aspx");
    }

    b.aspx中C#代码
    private void Page_Load(object sender, EventArgs e)
    {
        string name;
        name = Request.Cookie["name"].Value.ToString();
    }

      5.  使用Server.Transfer方法
        这个才可以说是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。
    a.aspx的C#代码
    public string Name
    {
        get{ return Label1.Text;}
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
        Server.Transfer("b.aspx");
    }

    b.aspx中C#代码
    private void Page_Load(object sender, EventArgs e)
    {
        a newWeb;   //实例a窗体
        newWeb = (source)Context.Handler;
        string name;
        name = newWeb.Name;
    }

  • 相关阅读:
    go函数
    Linux 查看磁盘容量、查找大文件、查找大目录
    五分钟理解一致性哈希算法(consistent hashing)
    使用Java实现三个线程交替打印0-74
    Python实现IOC控制反转
    Wannafly挑战赛5 A珂朵莉与宇宙 前缀和+枚举平方数
    Yandex Big Data Essentials Week1 Scaling Distributed File System
    Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
    Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration
    Yandex Big Data Essentials Week1 Unix Command Line Interface File System exploration
  • 原文地址:https://www.cnblogs.com/stevenxiao/p/340709.html
Copyright © 2011-2022 走看看