• 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"]来实现。

  • 相关阅读:
    关于tp5框架的安装与配置
    关于php的前台,后台的基本写法
    提取大段文字中的特殊段落
    UGUI 打图集
    事件管理
    tornado install
    Install aws cli
    code migrate
    Codecommit
    Curl elasticsearch
  • 原文地址:https://www.cnblogs.com/top5/p/1566686.html
Copyright © 2020-2023  润新知