.net之QueryString


asp.net中有四大跨页面传值的方法,利用QueryString是最简单的一种。

关于QueryString 有以下几个记忆点:

1.QueryString所传的值必须在2K以内,2K包括url地址

2.获取信息的形式只能是一个字符串

3.页面跳转同时存在Sever.Transfer和Response.Redirect时可能会存在bug

4.调用不存的参数的返回值,返回的是null,而不是空字符串

5,在保证与其他参数不会重名的时候,可以简略写成Request["paraname"];


以下是针对上面各点的小程序:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        静态链接传值:<br />
        <a href="Default2.aspx?id=123">
        ID=123</a></div>
    <p>
        控件传值:</p>
    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="发送" οnclick="Button1_Click" />
    </p>
    <p>
        checkbox传值:</p>
    <p>
        <asp:CheckBox ID="CheckBox1" runat="server" 
            oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
    </p>
    <p>
        传多个值:<asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>阿里</asp:ListItem>
            <asp:ListItem>小米</asp:ListItem>
            <asp:ListItem>百度</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem>马云</asp:ListItem>
            <asp:ListItem>李彦宏</asp:ListItem>
            <asp:ListItem>雷军</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button2" runat="server" Text="发送" οnclick="Button2_Click" />
    </p>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name_url;
        name_url = "Default2.aspx?name=" + TextBox1.Text;
        Response.Redirect(name_url);
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        string _chk = CheckBox1.Checked.ToString();
        string _url = "Default2.aspx?_chk=" + _chk;
        Server.Transfer(_url);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string _name = DropDownList2.SelectedItem.Text;
        string _website = DropDownList1.SelectedItem.Text;
        string _url = "Default2.aspx?_name=" + _name + "&_website=" + _website;
        Response.Redirect(_url);
    }
}

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        接受静态链接的传值:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    
    </div>
    <p>
        接收控件传值:<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </p>
    <p>
        接收checkbox的传值:<asp:Label ID="Label3" runat="server" Text=""></asp:Label>
    </p>
    <p>
        接收多个传值:<asp:Label ID="Label4" runat="server" Text=""></asp:Label>
        <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
    </p>
    </form>
</body>
</html>

Default2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.QueryString["id"];
       Label2.Text = Request.QueryString["name"];
        Label3.Text = Request.QueryString["_chk"];
        Label4.Text = Request.QueryString["_name"];
        Label5.Text = Request.QueryString["_website"];
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值