码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET页面之间传值的方式之QueryString(超详细)

时间:2017-10-19 21:17:17      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:asp.net   联系   ebe   按钮   数据   controls   http   meta   .net   

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

 

ASP.NET页面之间传值的方式之QueryString(超详细)

标签:asp.net   联系   ebe   按钮   数据   controls   http   meta   .net   

原文地址:http://www.cnblogs.com/kudsu/p/7694637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!