使用ViewState在Listview上应用过滤器

时间:2016-07-19 09:08:40

标签: asp.net vb.net listview filtering

我使用ListView Control从数据库中获取产品列表。我还将结果存储在viewstate中。现在要从复选框应用过滤器来获取精炼数据,我想知道如何使用viewState值?

e.g。如果页面加载时在音乐类别中找到10个产品现在,如果用户应用过滤器(蓝牙),那么只显示那些在音乐和音频中的产品。有蓝牙..

现在正在工作就像在页面加载音乐类别被获取然后如果我检查蓝牙过滤器然后所有蓝牙产品来到与音乐无关。

Private Sub shop_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim pageName As String = Me.Page.RouteData.Values("category").ToString()
    if not Me.isPostback Then
     Try
            query = select * from products where category = '"+pageName+"'
            Dim conString As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
            Dim con As New MySqlConnection(conString)
            Dim cmd As New MySqlCommand(query)
            con.Open()
            Dim da As New MySqlDataAdapter()
            cmd.Connection = con
            da.SelectCommand = cmd
            Dim dt As New DataTable()
            da.Fill(dt)
            ViewState("Data") = dt
            products.DataSource = dt
            products.DataBind()
            catHeading.Text = pageName
            itemCount.Text = dt.Rows.Count.ToString
            con.Close()
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End If
End Sub

过滤应用代码

Private Sub priceFilter_SelectedIndexChanged(sender As Object, e As EventArgs) Handles priceFilter.SelectedIndexChanged
    'buildWhereClause()

    Dim price As String = priceFilter.SelectedValue.ToString()
    Dim dt As DataTable = DirectCast(ViewState("Data"), DataTable)
    Dim dr As DataRow() = dt.[Select]((Convert.ToString("category='") & price) + "'")
    products.DataSource = dt
    products.DataBind()
    itemCount.Text = dt.Rows.Count.ToString
End Sub

我只想在用户应用任何过滤器时,它应该从viewstate(Data)检查而不是整个表。

2 个答案:

答案 0 :(得分:1)

将您的类别保存在viewstate& on Checked在字符串&中获取该类别在查询中加入该字符串。像这样的东西

    Dim constr As String = ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString
    Dim query As String = "select * from table"
    Dim joiner As String = ""

    Dim condition As String = String.Empty
    Dim whereClause As String = String.Empty
    Dim priceCondition As String = String.Empty
    Try
        Dim category As String = ViewState("Data")
        condition = String.Concat(condition, joiner, String.Format("{0}", category))
        If joiner = "" Then joiner = ""


        joiner = " where "
        If Not String.IsNullOrEmpty(condition) Then
            whereClause = String.Concat(whereClause, joiner, String.Format("category Like '%{0}%'", condition))
            joiner = " and "
        End If


        'Same way you can apply multiple filters as you want & then get that in one string like below

        Dim masterClause As String = String.Empty
        masterClause = (query & whereClause)

        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand(masterClause)
                Using sda As New MySqlDataAdapter(cmd)
                    cmd.Connection = con
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        products.DataSource = dt
                        products.DataBind()
                        itemCount.Text = dt.Rows.Count.ToString
                    End Using
                End Using
            End Using
        End Using

答案 1 :(得分:0)

对于您的过滤器,您可以使用:

    Dim dt As DataTable = DirectCast(ViewState("Data"), DataTable)
    Dim dr As DataRow() = dt.Select("category='" & category & "'")
    products.DataSource = dr
    products.DataBind()
    itemCount.Text = dr.Length