gpt4 book ai didi

mysql - 应用 onPage 过滤器时对象引用未设置到对象的实例

转载 作者:行者123 更新时间:2023-11-29 02:18:04 25 4
gpt4 key购买 nike

这似乎是非常基本的问题,但无法解决。我有 ListView 控件和一些过滤器作为单选按钮列表。现在,当页面加载时,它会正确显示数据。但是当我尝试对其应用过滤器时,它显示异常“对象引用未设置为对象的实例”。并且还请建议是否可以在过滤器上应用任何简化的方法来减少我当前的编码长度。

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

这里我使用了预呈现事件,因为我在母版页中有下拉列表并且我从母版页获取它的控制权。到这里一切正常。

当我尝试在其中应用页面过滤器时出现问题。以下是代码

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

最佳答案

我检查了代码,发现 productList 下拉列表有界属性,但不知何故它没有维护选定的索引。

所以请尝试如下,

productList.SelectedIndex = 0

这将解决问题。

关于mysql - 应用 onPage 过滤器时对象引用未设置到对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com