1. 程式人生 > >ASP.NET頁面之間傳值的方式之QueryString(超詳細)

ASP.NET頁面之間傳值的方式之QueryString(超詳細)

res p s orm font utf-8 但是 partial .aspx object

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(超詳細)