I have a gridview which can be filtered from one or more values in a querystring. That all works great: e.g. "?subject=Maths&authorName=Bond_James&type=Magazine"
The values passed to the query string come from 3 drop down lists: Subject, Author, Type. What I'd like is when the user presses "Filter" it will take the selected values from the drop down lists and pass them to the 开发者_如何学Pythonquerystring - it could be 1 value, 2, or all 3 (like above).
The drop down lists have an item called "All Subjects" / "All Author" / "All Type" each with a value of -1. The idea being that if the user leaves these items selected then the Filter button just ignores them.
Here is my code so far:
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
Also, one more thing. How do I get the drop down lists to have the filters selected when the page re loads?
I'd really appreciate any help!
EDIT: I changed the default values of the drop down lists to "" - this leaves the URL looking messy though ?author=&subject=&type=
This works, is it the best way?
I am not a VB person, so forgive any syntax errors. This approach will only add the query string if there are values, and only add terms for those parameters with values. I used "!="as the Not Equal operator. I am not sure if VB uses "<>" instead. Please consider this more as pseudo-code to illustrate the idea.
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
You can continue to use your empty string value and I would tend to prefer this to an assigned value, even though -1 is usually a good choice since it's highly unlikely that one of your filters would be using that as a value. I would clean up your query string though by calling something like the following function:
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
instead of this line from your code: queryString = selectedAuthorString & "&" & selectedSubjectString & "&" & selectedTypeString
Your resulting code would look like this:
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
And your query string won't contain filters that aren't applicable.
精彩评论