这似乎是非常基本的问题,但无法解决它。我有listview控件&一些过滤器作为radiobuttonlist。现在页面加载时它会正确显示数据。但是当我尝试在其上应用过滤器时,它会显示异常“对象引用未设置为对象的实例”。并且还请建议是否可以在过滤器上应用任何简化方法来减少当前的编码长度。
vb codePreRender事件
Private Sub chemists_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
Catch ex As Exception
Response.Write(ex)
End Try
Me.PopulateAreas()
Try
If Not Session("Data") Is Nothing Then
chemists.DataSource = Session("Data")
chemists.DataBind()
Else
Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
query = "SELECT chemistID, name, address, thumbnail, slot1From, slot1To, products FROM chemists where city like '" + citySelector.SelectedItem.ToString + "%' and status = 'active'"
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
chemists.DataSource = table
chemists.DataBind()
End If
'mainCount.Text = table.Rows(0)("countRows").ToString
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
这里我使用了prerender事件,因为我在母版页中有下拉列表&我从母版页获得它的控制权。直到这里一切正常。
当我尝试在其中应用页面过滤器时出现问题。以下是代码
Private Sub areasList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles areasList.SelectedIndexChanged
Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
locationFilter.Text = areasList.SelectedItem.ToString
city.Text = citySelector.SelectedItem.ToString
area.Text = areasList.SelectedIndex.ToString
Try
Session("Data") = Nothing
If areasList.SelectedValue <> "All" And productList.SelectedValue <> "All" And paymentsList.SelectedValue <> "All" Then
query = "SELECT * FROM chemists where address Like '%" + areasList.SelectedItem.ToString + "%' and products Like '%" + productList.SelectedItem.ToString + "%' and payment Like '%" + paymentsList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active' "
ElseIf areasList.SelectedValue = "All" And productList.SelectedValue <> "All" And paymentsList.SelectedValue.ToString <> "All" Then
query = "SELECT * FROM chemists where products Like '%" + productList.SelectedItem.ToString + "%' and payment Like '%" + paymentsList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
ElseIf areasList.SelectedValue = "All" And productList.SelectedValue = "All" And paymentsList.SelectedValue.ToString <> "All" Then
query = "SELECT * FROM chemists WHERE payment Like '%" + paymentsList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
ElseIf areasList.SelectedValue <> "All" And productList.SelectedValue = "All" And paymentsList.SelectedValue.ToString = "All" Then
query = "SELECT * FROM chemists WHERE address Like '%" + areasList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
ElseIf areasList.SelectedValue <> "All" And productList.SelectedValue <> "All" And paymentsList.SelectedValue.ToString = "All" Then
query = "SELECT * FROM chemists WHERE address Like '%" + areasList.SelectedItem.ToString + "%' and products Like '%" + productList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
ElseIf areasList.SelectedValue = "All" And productList.SelectedValue <> "All" And paymentsList.SelectedValue.ToString = "All" Then
query = "SELECT * FROM chemists WHERE products Like '%" + productList.SelectedItem.ToString + "%' and city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
ElseIf areasList.SelectedValue = "All" And productList.SelectedValue = "All" And paymentsList.SelectedValue = "All" Then
query = "SELECT * FROM chemists where city = '" + citySelector.SelectedItem.ToString + "' and status = 'active'"
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
chemists.DataSource = table
chemists.DataBind()
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
答案 0 :(得分:2)
我检查了代码并发现productList
下拉有界属性,但不知何故它没有维护选定的索引。
所以请尝试如下,
productList.SelectedIndex = 0
这将解决问题。