通过选择ComboBox来过滤listview数据

时间:2015-03-03 06:58:01

标签: asp.net vb.net

我正在学习ASP.Net开发&尝试创建Web应用程序,其中ListView数据应该在ComboBox中完成筛选器选择。我制作了ListView&将它绑定到我的数据库表&试图把ComboBox过滤器但是得到这个错误

  输出中的

(System.Web.HttpException:请求在此上下文中不可用)。

VB代码

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class VB
    Inherits System.Web.UI.Page
    Dim query As String
    Dim strArea As String = Request.QueryString("Country")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.BindListView()
        End If
    End Sub

    Private Sub BindListView()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand()
                cmd.CommandText = "SELECT CustomerId, ContactName, Country FROM Customers"
                cmd.Connection = con
                Using sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    lvCustomers.DataSource = dt
                    lvCustomers.DataBind()
                End Using
            End Using
        End Using
    End Sub

    Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
        TryCast(lvCustomers.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
        Me.BindListView()
    End Sub

    Protected Sub country_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles country.SelectedIndexChanged
        Try
            If country.SelectedValue <> "All" Then
                query = "Select CutomerId, Contact Name, Country from Customers where Country like '%" + strArea + "%' order by rank;"
            End If
            Dim cmd As New SqlCommand(query)
            Dim da As New SqlDataAdapter(cmd)
            Dim table As New DataTable
        Catch ex As Exception

        End Try

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

Request只读属性属于Page类,并且继承的类成员仅在继承它的类的成员中可用(在您的情况下为VB)。像这样更改你的代码: -

Partial Class VB
    Inherits System.Web.UI.Page
    Dim query As String
    Dim strArea As String = String.Empty

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.BindListView()
        End If
        strArea  = Request.QueryString("Country")
    End Sub

另外,请注意最好在分配之前检查查询字符串变量中是否存在任何值。