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

 

转载于:https://www.cnblogs.com/kudsu/p/7694637.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值