0

我正在尝试在 VB.NET 中初始化一个下拉列表,但我的下拉列表没有填充任何值。我希望 DataTextField 与下拉列表中的 DataValues 不同。下拉列表应显示一系列字符串;DataValues 只能是数字。

我该如何实施?这是我现在的代码摘录:

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()
4

2 回答 2

1

你能用这样的东西吗?

While reader.Read()
   Dim ListItem as new ListItem
   ListItem.Text = reader(0)
   ListItem.Value = reader(1)
   dProdCodeSearch.Items.Add(ListItem)
End While

它通常对我有用..

于 2011-08-17T18:27:25.377 回答
0

尝试如下更改您的代码,我假设您使用的是 ASP.NET

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()
于 2011-08-15T13:17:20.543 回答