使用radiobutton选择过滤listview行

时间:2016-03-21 06:01:44

标签: asp.net vb.net listview

我不明白我在这里做了什么错,但我的列表视图并没有根据选择过滤数据。一旦页面加载,它将从主页面根据所选城市获取数据。这是有效的,但是当我应用另一个过滤器时,它不会被改变。在页面上我有多个过滤器。

VB

Private Sub hospitals_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
    Try
        Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
        If Session("masterLocation") Is Nothing Then
            citySelector.Text = "Pune"
        Else
            citySelector.Text = Session("masterLocation").ToString()
        End If

        query = "SELECT hospitalID, name, address, thumbnail, knownFor, mondayFrom, mondayTo, consultancyFees FROM hospitals where city like '" + citySelector.SelectedItem.ToString + "%'"
        Dim cmd As New MySqlCommand(query, con)
        cmd.CommandTimeout = 120
        Dim da As New MySqlDataAdapter(cmd)
        Dim table As New DataTable
        da.Fill(table)
        ViewState("Data") = table
        hospitals.DataSource = table
        hospitals.DataBind()
        'countItems.Text = String.Format(table.Rows(0)("dataCount"))
    Catch ex As Exception
        Response.Write(ex)
    End Try
End Sub

Private Sub areasList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles areasList.SelectedIndexChanged
    'locationFilter.Text = areasList.SelectedItem.ToString
    Try
        ViewState("Data") = ""
        If areasList.SelectedValue <> "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue <> "All" And paymentsList.SelectedValue <> "All" Then
            query = "SELECT * FROM hospitals where address Like '" + areasList.SelectedItem.ToString + "%' and services Like '%" + servicesList.SelectedItem.ToString + "%' and facilities Like '%" + facilitiesList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue = "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue <> "All" And paymentsList.SelectedValue <> "All" Then
            query = "SELECT * FROM hospitals where services Like '%" + servicesList.SelectedItem.ToString + "%' and facilities Like '%" + facilitiesList.SelectedItem.ToString + "%' and payment Like '%" + paymentsList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue <> "All" And servicesList.SelectedValue = "All" And facilitiesList.SelectedValue <> "All" And paymentsList.SelectedValue <> "All" Then
            query = "SELECT * FROM hospitals where areaName Like '%" + areasList.SelectedItem.ToString + "%' and facilities Like '%" + facilitiesList.SelectedItem.ToString + "%' and payment Like '%" + paymentsList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue <> "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue <> "All" Then
            query = "SELECT * FROM hospitals where areaName Like '%" + areasList.SelectedItem.ToString + "%' and services Like '%" + servicesList.SelectedItem.ToString + "%' and payment Like payment Like '%" + paymentsList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue <> "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue <> "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals where areaName Like '%" + areasList.SelectedItem.ToString + "%' and services Like '%" + servicesList.SelectedItem.ToString + "%' and facilities Like '%" + facilitiesList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue <> "All" And servicesList.SelectedValue = "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals where areaName Like '%" + areasList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue = "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals where services Like '%" + servicesList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue = "All" And servicesList.SelectedValue = "All" And facilitiesList.SelectedValue <> "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals where facilities Like '%" + facilitiesList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue = "All" And servicesList.SelectedValue = "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue <> "All" Then
            query = "SELECT * FROM hospitals where payment Like '%" + paymentsList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue <> "All" And servicesList.SelectedValue <> "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals where areaName Like '%" + areasList.SelectedItem.ToString + "%' and services Like '%" + servicesList.SelectedItem.ToString + "%'"

        ElseIf areasList.SelectedValue = "All" And servicesList.SelectedValue = "All" And facilitiesList.SelectedValue = "All" And paymentsList.SelectedValue = "All" Then
            query = "SELECT * FROM hospitals WHERE status Like 'Active'"
        End If

        Dim cmd As New MySqlCommand(query, con)
        Dim da As New MySqlDataAdapter(cmd)
        Dim table As New DataTable

        da.Fill(table)
        ViewState("Data") = table
        hospitals.DataSource = table
        hospitals.DataBind()
    Catch ex As Exception
        'Response.Write(ex)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您遇到的问题取决于您的工作地点。 您的代码中存在许多问题。 首先,我建议您在onload / Page_Load上移动预渲染上的代码 在那种方法中你有

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);  
  // here code executes Always on every onload
  if (Page.IsPostback)
        return;
  // here code executes only first load
  FillCombo();
  SetInitialValues();
  BindSearchResults();
}

我也建议:

  1. 移动数据层类中的所有查询
  2. 更喜欢零或“”到“全部”
  3. 写入Status ='Active'而不是
  4. 我多次阅读Page.Master.FindControl(“locationSelector”):我认为有更好的方法来获取locationSelector。如果它位于ascx控件或母版页中,则可以公开更改。