下面我们创建两个页面测试QueryString 页面间的传值。
set.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_02.aspx.cs" Inherits="Sample_02" %>
<!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>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>QueryString传值</h3>
<p><span class="spMemo">静态超链接传值:</span>
<a href="Sample_02_targ.aspx?id=12345">ID=12345</a>
</p>
<p><span class ="spMemo">获取txt_name控件的值,通过Response.Redirect转向并传值:</span><br />
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_name" runat="server" Text="传递name"
οnclick="btn_name_Click" />
</p>
<p><span class="spMemo">获取checkbox的值,并通过Server.Transfer转向并传至:(AutoPostBack)</span><br />
<asp:CheckBox ID="chk_1" runat="server" Text="传递是否选中" AutoPostBack="True"
oncheckedchanged="chk_1_CheckedChanged" />
</p>
<p><span class ="spMemo">一次性传递多个值:</span>
<asp:DropDownList ID="ddl_soft" runat="server">
<asp:ListItem>ASP.NET</asp:ListItem>
<asp:ListItem>JavaScript</asp:ListItem>
<asp:ListItem>CSS</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_website" runat="server">
<asp:ListItem>百度</asp:ListItem>
<asp:ListItem>google</asp:ListItem>
<asp:ListItem>163</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btn_muilt" runat="server" Text="Button"
οnclick="btn_muilt_Click" />
</p>
</div>
</form>
</body>
</html>
设计图如下:
set.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 Sample_02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void chk_1_CheckedChanged(object sender, EventArgs e)
{
string _checked = chk_1.Checked.ToString();
string _url = "Sample_02_targ.aspx?chk=" + _checked;
Server.Transfer(_url);
}
protected void btn_name_Click(object sender, EventArgs e)
{
string _name = txt_name.Text;
string _url = "Sample_02_targ.aspx?name=" + _name;
Response.Redirect(_url);
}
protected void btn_muilt_Click(object sender, EventArgs e)
{
string _soft = ddl_soft.Text, _website = ddl_website.Text;
//string _url = "Sample_02_targ.aspx?soft=" + _soft + "&website=" + _website;
string _url = "Sample_02_targ.aspx?soft="+_soft+"&website="+_website;
Response.Redirect(_url);
}
}
read.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_02_targ.aspx.cs" Inherits="Sample_02_targ" %>
<!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>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>QueryString传值,接收值页面</h3>
<p>接收ID值,赋值给LABEL:
<asp:Label ID="lab_id" runat="server" ></asp:Label>
</p>
<p>接收name值,赋值给LABEL:
<asp:Label ID="lab_name" runat="server" ></asp:Label>
</p>
<p>接收chk值,赋值给LABEL:
<asp:Label ID="lab_chk" runat="server" ></asp:Label>
</p>
<p>接收SOFT和WEBSITE值,赋值给LABEL:
<asp:Label ID="lab_soft" runat="server" ></asp:Label>,
<asp:Label ID="lab_website" runat="server" ></asp:Label>
</p>
</div>
</form>
</body>
</html>
设计图如下:
read.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 Sample_02_targ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//在PageLoad中接收传递来的值
//接收ID值
//当没有参数时QueryString["id"] == null
lab_id.Text = Request.QueryString["id"];
//接收NAME值
lab_name.Text = Request.QueryString["name"];
//接收CHK值
lab_chk.Text = Request.QueryString["chk"];
//接收soft,website值,采用简略的写法
lab_soft.Text = Request["soft"];
lab_website.Text = Request["website"];
}
}
效果如下: