vb.net - VB.Net - 如何让 DropDownList 根据 URL 查询字符串自动选择数据?
问题描述
在我的 Web 表单中,我的下拉列表已经包含加载数据选择的代码。现在我想根据 URL 中的查询字符串添加一个自动填充功能。
Private Sub FillLine() '1st dropdown
'1. Load intial selection into drop down list (OK)
'----------------------------------------------------------------------------------------------------
Dim dt As New DataTable
Dim strSql = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
& " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
line.DataSource = dt
line.DataTextField = "LCODE1"
line.DataValueField = "level1_id"
line.DataBind()
line.Items.Insert(0, "")
End If
'----------------------------------------------------------------------------------------------------
'2. Pick out the value in drop down list based on URL query string (NG)
'----------------------------------------------------------------------------------------------------
Dim LID As String = Request.QueryString("LID")
If LID <> Nothing Then
LID3.Text = LID
If Not IsPostBack() Then
Dim cmdLID As New SqlCommand("select distinct [LCODE1]+ ' | '+[LNAME1] as [LCODE1] " _
& "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] " _
& "where [LEVEL3_ID] = '" & LID3.Text & "'", conn)
conn.Open()
Dim rdr As SqlDataReader = cmdLID.ExecuteReader
While rdr.Read
line.Text = rdr("LCODE1")
End While
conn.Close()
End If
End If
'----------------------------------------------------------------------------------------------------
End Sub
Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString())
Dim value As Integer = Integer.Parse(level1_ID)
FillProcess(level1_ID)
Catch ex As Exception
Console.WriteLine(ex)
End Try
'FillProcess(level1_ID)
End Sub
在测试期间,我的代码的第 1 部分很好。但是,在第 2 部分中,每次我LID
在 URL 中输入查询字符串并刷新页面时,该值都不会显示在我的下拉列表中。
这里可能是什么问题?
解决方案
我正在使用我的一个数据库进行测试。第 1 部分与您的代码相同。
第 2 部分
无需再次访问数据库。您需要的值在 DropDownList 中。我对 LID 使用硬编码值进行测试,但只需替换您的查询字符串值。循环遍历列表中的项目,直到找到您要查找的值。
Private Sub FillDropDownList()
'1. Load intial selection into drop down list (OK)
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Top 10 ID, Name From Coffees", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
DropDownList1.DataTextField = "Name"
DropDownList1.DataValueField = "ID"
DropDownList1.DataSource = dt
DropDownList1.DataBind()
'2. Pick out the value in drop down list based on URL query string
Dim LID As String = "14" 'Request.QueryString("LID")
For Each item As ListItem In DropDownList1.Items
If item.Value = LID Then
item.Selected = True
Exit For
End If
Next
End Sub
推荐阅读
- node.js - 重新启动计算机后 Nodejs 出现错误
- javascript - 将 json 对象解析为数组
- javascript - 使用 setInterval Node.js 每秒加载新的 HTML 文件
- html - 使用 Bootstrap 为 Web 和移动设备创建可滑动的导航栏
- reactjs - 守望者意外退出
- html - 如何设置网格元素旋转
- azure - 如何通过另一个逻辑应用访问 Azure 逻辑应用运行日志
- javascript - 有没有办法使用命名箭头函数来维护对象内的词法范围?
- hyperledger-fabric - 在 Hyperledger Fabric 中使用基于自定义关联的策略
- java - 在读取 excel 文件时继续在 java 中进行下一次迭代