zoukankan      html  css  js  c++  java
  • ASP.NET实现页面传值的几种方法

    第一种方法

    通过URL链接地址传递

    以下为引用的内容:
    send.aspx:
      protected void Button1_Click(object sender, EventArgs e)
        {
            Request.Redirect("Default2.aspx?username=honge");
        }
    receive.aspx:
    string username = Request.QueryString["username"];这样可以得到参数值。

    第二种方法:

    通过post方式。

    以下为引用的内容:

    send.aspx

    receive.aspx
    string username = Ruquest.Form["receive"];

    第三种方法:

    以下为引用的内容:

    通过session

    send.aspx:
      protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = "honge";
            Request.Redirect("Default2.aspx");
        }
    receive.aspx:
    string username = Session["username"];这样可以得到参数值。

    第四种方法:

    以下为引用的内容:

    通过Application

    send.aspx:
      protected void Button1_Click(object sender, EventArgs e)
        {
            Application["username"] = "honge";
            Request.Redirect("Default2.aspx");
        }
    receive.aspx:
    string username = Application["username"];这样可以得到参数值。

    第五种方法:

    通过Server.Transfer

    以下为引用的内容:
    send.aspx:
      public string Name
        {
            get {
                return "honge";
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("Default2.aspx");
        }
    receive.aspx:
       send d = Context.Handler as send ;
            if (d != null)
            {
                Response.Write(d.Name);这样可以得到参数值。
            }

    如果在asp.net 2.0中还可以这样用:通过PreviousPage

    以下为引用的内容:
    PreviousPage d = Context.Handler as PreviousPage ;
    if (d != null)
    {
    Response.Write(d.Name);这样可以得到参数值。
    }

    也可以这样用:

    以下为引用的内容:

    send.aspx:

    receive.aspx:

    string name = PreviousPage.Name;这样可以得到参数值。

    如果你的页面中用到了MasterPage的话 Server.Transfer 传递的 PreviousPage就无效了,不知道这是什么原因.所以在用到MasterPage的话,最好用Session或是 Context.Items["username"]来实现。

  • 相关阅读:
    JZYZOJ1311 邮局设置问题 dp
    备忘录和乱七八糟的体会
    Python strip、lstrip和rstrip的用法
    Linux jstack命令详解
    Linux EOF使用
    Linux 千万不要执行的10个命令
    Linux 浅谈Linux 操作系统的安全设置
    linux 使用 ionice 限制 Xen 虚拟机磁盘 IO
    Linux 实现rsyslog日志里面的IP地址记录 未测试
    linux 回收站的添加
  • 原文地址:https://www.cnblogs.com/top5/p/1566686.html
Copyright © 2011-2022 走看看