2008-09-17 157 views
0

我想根據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 

是這是構建查詢字符串的最佳方式,還是應該採用不同的方法?感謝我能得到的所有幫助。非常感謝。

回答

1

最簡單的方法是使用非服務器端<表格>標籤與方法=「獲取」然後當表單被提交時,你會自動得到你查詢字符串(並且不要忘記添加<標籤>標籤,並將它們與單選按鈕關聯):

<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> 
0

您可以使用StringBuilder而不是創建這三個不同的字符串。您可以通過預先分配您需要存儲字符串的內存量來幫助解決問題。您也可以使用String.Format。

如果這是你所有的提交按鈕正在做爲什麼使它成爲一個.Net頁面,而只是有一個GET表單轉到nextpage.aspx進行處理?

+0

請記住,包括System.Text如果你想使用StringBuilder。 – 2008-09-17 11:04:34