从下拉列表创建多个查询字符串 - 可以是1,2或3个查询字符串。我该怎么做?

时间:2011-02-19 16:51:51

标签: asp.net vb.net query-string

我有一个gridview,可以从查询字符串中的一个或多个值进行过滤。一切都很好:例如"?subject=Maths&authorName=Bond_James&type=Magazine"

传递给查询字符串的值来自3个下拉列表:Subject,Author,Type。 我想要的是当用户按下“过滤器”时,它将从下拉列表中选择所选值并将它们传递给查询字符串 - 它可以是1个值,2个或全部3个(如上所述)。

下拉列表中有一个名为“All Subjects”/“All Author”/“All Type”的项目,每个项目的值为-1。我们的想法是,如果用户选择了这些项目,那么“过滤器”按钮就会忽略它们。

到目前为止,这是我的代码:

  Protected Sub buttonFilterGo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonFilterGo.Click

    Dim queryString As String
    Dim selectedAuthorString As String = "author=" & dropAuthorList.SelectedItem.Value
    Dim selectedSubjectString As String = "subject=" & dropSubjectList.SelectedItem.Value
    Dim selectedTypeString As String = "type=" & dropPaperType.SelectedItem.Value
    Const PATH As String = "~/paper/browse/?"






    queryString = selectedAuthorString & "&" & selectedSubjectString & "&" & selectedTypeString
    If IsNothing(queryString) Then
        labelFilterFeedback.Text = "Apply some filters then press Go"
    Else
        Response.Redirect(PATH & queryString)
        labelFilterFeedback.Text = ""
    End If


End Sub

另外,还有一件事。如何在页面重新加载时获取下拉列表以选择过滤器?

我真的很感激任何帮助!

编辑:我将下拉列表的默认值更改为“” - 这使得网址看起来很混乱?author=&subject=&type=这样可行,这是最好的方法吗?

2 个答案:

答案 0 :(得分:0)

你可以继续使用你的空字符串值,我倾向于将它更喜欢指定的值,即使-1通常是一个不错的选择,因为你的一个过滤器很可能不会将其用作值。我会通过调用以下函数来清理你的查询字符串:

Private Function AppendFilter(ByVal filterName As String, ByVal filterVal As String, ByVal query As String) As String
    Dim res As String = query

    If filterVal.Length() > 0 Then 
        res = IIf(query.Length() > 0, query & "&", query) & filterName & "=" & filterVal
    End If

    Return res
End Function

而不是代码中的这一行:queryString = selectedAuthorString & "&" & selectedSubjectString & "&" & selectedTypeString

您生成的代码如下所示:

Protected Sub buttonFilterGo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonFilterGo.Click

    Dim queryString As String = ""
    Const PATH As String = "~/paper/browse/?"

    queryString = AppendFilter("author", dropAuthorList.SelectedItem.Value, queryString)
    queryString = AppendFilter("subject", dropSubjectList.SelectedItem.Value, queryString)
    queryString = AppendFilter("type", dropPaperType.SelectedItem.Value, queryString)

    If queryString.Length() <= 0 Then
        labelFilterFeedback.Text = "Apply some filters then press Go"
    Else
        Response.Redirect(PATH & "?" & queryString)
        labelFilterFeedback.Text = ""
    End If
End Sub

您的查询字符串将不包含不适用的过滤器。

答案 1 :(得分:0)

我不是VB人,所以请原谅任何语法错误。如果存在值,此方法将仅添加查询字符串,并且仅为具有值的参数添加术语。我使用“!=”作为Not Equal运算符。我不确定VB是否使用“&lt;&gt;”代替。请将此视为伪代码来说明这个想法。

Dim queryString As String = "?"
...
 Const PATH As String = "~/paper/browse/"
...

If dropAuthorList.SelectedItem.Value != "" Then
   queryString = queryString & selectedAuthorString 
EndIf
If dropSubjectList.SelectedItem.Value != "" Then
   If querystring.Length > 1 Then
        queryString = queryString + "&"
   EndIf
   queryString = queryString & selectedSubjectString 
EndIf
If dropPaperType.SelectedItem.Value != "" Then
   If querystring.Length > 1 Then
        queryString = queryString + "&"
   EndIf
   queryString = queryString & selectedTypeString 
EndIf
If querystring.Length = 1 Then
    queryString = ""
EndIf