找回密码
 会员注册
查看: 120|回复: 0

asp.net页面传值之QueryString

[复制链接]

250

主题

1

回帖

819

积分

管理员

积分
819
发表于 2024-2-29 08:20:21 | 显示全部楼层 |阅读模式

下面我们创建两个页面测试QueryString 页面间的传值。

set.aspx代码如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_02.aspx.cs" Inherits="Sample_02" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <h3>QueryString传值</h3>
  12. <p><span class="spMemo">静态超链接传值:</span>
  13. <a href="Sample_02_targ.aspx?id=12345">ID=12345</a>
  14. </p>
  15. <p><span class ="spMemo">获取txt_name控件的值,通过Response.Redirect转向并传值:</span><br />
  16. <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
  17. <asp:Button ID="btn_name" runat="server" Text="传递name"
  18. οnclick="btn_name_Click" />
  19. </p>
  20. <p><span class="spMemo">获取checkbox的值,并通过Server.Transfer转向并传至:(AutoPostBack)</span><br />
  21. <asp:CheckBox ID="chk_1" runat="server" Text="传递是否选中" AutoPostBack="True"
  22. oncheckedchanged="chk_1_CheckedChanged" />
  23. </p>
  24. <p><span class ="spMemo">一次性传递多个值:</span>
  25. <asp:DropDownList ID="ddl_soft" runat="server">
  26. <asp:ListItem>ASP.NET</asp:ListItem>
  27. <asp:ListItem>JavaScript</asp:ListItem>
  28. <asp:ListItem>CSS</asp:ListItem>
  29. </asp:DropDownList>
  30. <asp:DropDownList ID="ddl_website" runat="server">
  31. <asp:ListItem>百度</asp:ListItem>
  32. <asp:ListItem>google</asp:ListItem>
  33. <asp:ListItem>163</asp:ListItem>
  34. </asp:DropDownList>
  35. <asp:Button ID="btn_muilt" runat="server" Text="Button"
  36. οnclick="btn_muilt_Click" />
  37. </p>
  38. </div>
  39. </form>
  40. </body>
  41. </html>
复制代码

设计图如下:


set.aspx.cs代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class Sample_02 : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void chk_1_CheckedChanged(object sender, EventArgs e)
  13. {
  14. string _checked = chk_1.Checked.ToString();
  15. string _url = "Sample_02_targ.aspx?chk=" + _checked;
  16. Server.Transfer(_url);
  17. }
  18. protected void btn_name_Click(object sender, EventArgs e)
  19. {
  20. string _name = txt_name.Text;
  21. string _url = "Sample_02_targ.aspx?name=" + _name;
  22. Response.Redirect(_url);
  23. }
  24. protected void btn_muilt_Click(object sender, EventArgs e)
  25. {
  26. string _soft = ddl_soft.Text, _website = ddl_website.Text;
  27. //string _url = "Sample_02_targ.aspx?soft=" + _soft + "&website=" + _website;
  28. string _url = "Sample_02_targ.aspx?soft="+_soft+"&website="+_website;
  29. Response.Redirect(_url);
  30. }
  31. }
复制代码

read.aspx代码如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_02_targ.aspx.cs" Inherits="Sample_02_targ" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <h3>QueryString传值,接收值页面</h3>
  12. <p>接收ID值,赋值给LABEL:
  13. <asp:Label ID="lab_id" runat="server" ></asp:Label>
  14. </p>
  15. <p>接收name值,赋值给LABEL:
  16. <asp:Label ID="lab_name" runat="server" ></asp:Label>
  17. </p>
  18. <p>接收chk值,赋值给LABEL:
  19. <asp:Label ID="lab_chk" runat="server" ></asp:Label>
  20. </p>
  21. <p>接收SOFT和WEBSITE值,赋值给LABEL:
  22. <asp:Label ID="lab_soft" runat="server" ></asp:Label>,
  23. <asp:Label ID="lab_website" runat="server" ></asp:Label>
  24. </p>
  25. </div>
  26. </form>
  27. </body>
  28. </html>
复制代码

设计图如下:


read.aspx.cs代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class Sample_02_targ : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. //在PageLoad中接收传递来的值
  12. //接收ID值
  13. //当没有参数时QueryString["id"] == null
  14. lab_id.Text = Request.QueryString["id"];
  15. //接收NAME值
  16. lab_name.Text = Request.QueryString["name"];
  17. //接收CHK值
  18. lab_chk.Text = Request.QueryString["chk"];
  19. //接收soft,website值,采用简略的写法
  20. lab_soft.Text = Request["soft"];
  21. lab_website.Text = Request["website"];
  22. }
  23. }
复制代码
效果如下:




来源:https://blog.csdn.net/yayun0516/article/details/41698879
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?会员注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-27 00:17 , Processed in 1.895475 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表