在listview中应用多个过滤器

时间:2016-04-04 04:54:02

标签: mysql asp.net vb.net listview

我有listview和onPage加载所有数据都显示在其中。要过滤listview,我有4种类型的RedioButtonList(区域,服务,设施,付款)。现在假设我应用第一个过滤器,即区域然后它工作正常,但之后我应用服务过滤器比区域变为空白。它应该在选定区域找到所选服务。调试后我看到实际传递的查询如下所示

select * from table where areaName = '' and services = 'selected service'

但它应该像这样

select * from table where areaName = 'SelectedArea' and services = 'selected service'

以下函数我在每个过滤器上运行IndexChanged

Private Sub getResult()
    Try
        Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
        Dim where As String = String.Empty
        Session("Data") = Nothing
        If areasList.SelectedValue <> "All" Then
            where = "address like '%" & areasList.SelectedValue & "%'"
        End If

        If servicesList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "serviceID like '%" & servicesList.SelectedValue.ToString & "%'"
            Else
                where &= "and serviceID like '" & servicesList.SelectedValue.ToString & "%'"
            End If
        End If

        If facilitiesList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "facilities like '%" & facilitiesList.SelectedValue & "%'"
            Else
                where &= "and facilities like '" & facilitiesList.SelectedValue & "%'"
            End If
        End If

        If paymentsList.SelectedValue <> "All" Then
            If String.IsNullOrEmpty(where) Then
                where = "payment like '%" & paymentsList.SelectedValue.ToString & "%'"
            Else
                where &= "and payment like '%" & paymentsList.SelectedValue.ToString & "%'"
            End If
        End If

        Dim query As String = "SELECT hospitalID, name, address, thumbnail, knownFor, mondayFrom, mondayTo, consultancyFees FROM hospitals where city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
        If Not String.IsNullOrEmpty(where) Then
            query &= " and " & where
        End If

        Dim cmd As New MySqlCommand(query, con)
        Dim da As New MySqlDataAdapter(cmd)
        Dim table As New DataTable
        da.Fill(table)
        Session("Data") = table
        hospitals.DataSource = table
        hospitals.DataBind()
    Catch ex As Exception
        Response.Write(ex)
    End Try
End Sub

我做错了,请帮忙。

1 个答案:

答案 0 :(得分:0)

每次更改其中一个下拉框中的索引时,都会重置where子句。您需要创建一个对所有下拉框都通用的函数,这些函数将遍历每个下拉框的值并创建包含所有变量的位置。

dim where as string;        
Private Sub changeWherestatement(sender As Object, e As EventArgs) Handles    ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
where=string.empty
 If areasList.SelectedValue <> "All" Then
        where = "address like '%" & areasList.SelectedValue & "%'"
    End If
If servicesList.SelectedValue <> "All" Then
        If String.IsNullOrEmpty(where) Then
            where += " and serviceID like '%" & servicesList.SelectedValue.ToString & "%'"
        Else
            where += "and serviceID like '" & servicesList.SelectedValue.ToString & "%'"
        End If
    End If
Etc

End Sub