• 【学习笔记】ASP.NET页面之间传值的方式之QueryString


    QueryString

    Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL。如果要从A页面跳转到B页面,则可以用Request.Redirect(”B.aspx?参数名=参数值”);在页面跳转后用Ruquest[“参数名”]来接收参数。这种方法使用简单,不用服务器资源。但是很容易被篡改且不能传递对象,只有在通过URL 请求页时查询字符串才是可行的。

      这种方法的优点:1.使用简单,对于安全性要求不高时传递数字或是文本值非常有效。
      这种方法的缺点:1.缺乏安全性,由于它的值暴露在浏览器的URL地址中的。
              2.不能传递对象。

      使用方法:1.在源页面的代码中用需要传递的名称和值构造URL地址。
           2.在源页面的代码用Response.Redirect(URL);重定向到上面的URL地址中。
           3.在目的页面的代码使用Request.QueryString["name"];取出URL地址中传递的值。

           例子:(1)a.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a.aspx.cs" Inherits="Web.a" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="张君宝"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
    </body>
    </html>
    

               (2)a.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Web
    {
        public partial class a : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void Button1_Click(object sender, EventArgs e)
            {            
                var url = "b.aspx?name=" + Label1.Text;
                Response.Redirect(url);//点击Button按钮重定向到b页面
            }
        }
    }
    

           (3)b.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="Web.b" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>
    

        (4)b.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Web
    {
        public partial class b : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text= Request.QueryString["name"];
            }
        }
    }
    

      

    ps:此文章是本人参考网上内容加上自己的理解整合而成,如无意中侵犯了您的权益,请与本人联系。

  • 相关阅读:
    一道经典的线程间通信的编程题
    Windows Live Writer代码插件整理
    Squeeze Excitation Module 对网络的改进分析
    IGC(Interleaved Group Convolutions)
    探秘移动网络模型
    sparse_softmax_cross_entropy_with_logits
    四行公式推完神经网络BP
    视觉跟踪:MDnet
    tensorflow API _ 6 (tf.gfile)
    tensorflow API _ 4 (Logging with tensorflow)
  • 原文地址:https://www.cnblogs.com/kudsu/p/7694637.html
Copyright © 2020-2023  润新知