构建基于查询字符串的单选按钮值

时间:2020-03-05 18:58:28  来源:igfitidea点击:

我想基于从5组单选按钮中获取的值来构建查询字符串。

选择任何组都是可选的,因此我们可以选择集合A或者B或者同时选择两者。我将如何基于此构建查询字符串?我正在使用VB.NET 1.1

asp:Radiobuttonlist控件不喜欢空值,因此我求助于普通的html单选按钮。我的问题是如何将选定的值串成查询字符串

我现在有这样的事情:

HTML:

<input type="radio" name="apBoat" id="Apb1" value="1" /> detail1
<input type="radio" name="apBoat" id="Apb2" value="2" /> detail2

<input type="radio" name="cBoat" id="Cb1" value="1" /> detail1
<input type="radio" name="cBoat" id="Cb2" value="2" /> detail2

VB.NET

Public Sub btnSubmit_click(ByVal sender As Object, ByVal e As System.EventArgs)
  Dim queryString As String = "nextpage.aspx?"

  Dim aBoat, bBoat, cBoat bas String

  aBoat = "apb=" & Request("aBoat")
  bBoat = "bBoat=" & Request("bBoat")
  cBoat = "cBoat=" & Request("cBoat ")

  queryString += aBoat & bBoat & cBoat

  Response.Redirect(queryString)

End Sub

这是构建查询字符串的最佳方法还是应该完全采用其他方法?感谢所有我能得到的帮助。非常感谢。

解决方案

回答

我们可以使用StringBuilder而不是创建这三个不同的字符串。我们可以通过预先分配存储字符串所需的内存量来帮助解决问题。我们也可以改用String.Format。

如果这就是我们提交按钮的全部作用,那么为什么要将其设置为.Net页面,而仅拥有GET表单,请转到nextpage.aspx进行处理?

回答

最简单的方法是将非服务器端<form>标记与method =" get"一起使用,然后在提交表单时,我们将自动获得我们要查询的查询字符串(并且不要忘记添加<label>标签并将其与单选按钮相关联):

<form action="..." method="get">
    <input type="radio" name="apBoat" id="Apb1" value="1" /> <label for="Apb1">detail1</label>
    <input type="radio" name="apBoat" id="Apb2" value="2" /> <label for="Apb2">detail2</label>

    <input type="radio" name="cBoat" id="Cb1" value="1" /> <label for="Cb1">detail1</label>
    <input type="radio" name="cBoat" id="Cb2" value="2" /> <label for="Cb2">detail2</label>
</form>