代码之家  ›  专栏  ›  技术社区  ›  Lars Haugseth

基于radiobutton值构建查询字符串

  •  0
  • Lars Haugseth  · 技术社区  · 18 年前

    我想根据5组单选按钮的值构建一个查询字符串。

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

    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
    

    2 回复  |  直到 18 年前
        1
  •  1
  •   Ian Oxley    18 年前

    最简单的方法是使用非服务器端<表格>使用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>
    
        2
  •  0
  •   Jon    18 年前

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

    推荐文章