我试图在VB中初始化下拉列表。. NET,但我的下拉列表没有填充任何值。我希望DataTextField与下拉列表中的数据值不同。下拉列表应该显示一系列字符串;数据值只能是数字。
我如何实现这个?下面是我的代码摘录:
Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String
pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
Dim DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)
reader = DBCommand.ExecuteReader()
While reader.Read()
End While
dProdCodeSearch.Items.Add(reader(0))
dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
dProdCodeSearch.DataBind()
reader.Close()
你能用这样的东西吗?
While reader.Read()
Dim ListItem as new ListItem
ListItem.Text = reader(0)
ListItem.Value = reader(1)
dProdCodeSearch.Items.Add(ListItem)
End While
尝试更改您的代码如下,我假设您使用的是ASP。净
Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String
pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
Dim DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)
reader = DBCommand.ExecuteReader()
While reader.Read()
dProdCodeSearch.Items.Add(reader(0))
End While
dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
reader.Close()