这是我填写下拉列表的代码:
For i = 1 To Convert.ToInt32(count1)
etc.CommandText = "select Classification from schemaItemDetails.AssetCategory where ASC_ID = " & i & ""
Dim dra1 As SqlDataReader = etc.ExecuteReader
While (dra1.Read())
ddonecategory.Items.Add(dra1.GetString(0))
End While
dra1.Close()
Next
如何设置下拉列表的文字?因为当我使用此代码设置下拉列表的文本时:
ddonecategory.Text = "Toolings".Text
我遇到了这种错误:
'ddonecategory' has a SelectedValue which is invalid because it does not exist in the list of items.
答案 0 :(得分:0)
我不知道如何用vb编写,但在C#中它应该是这样的
ddonecategory.SelectedIndex =
ddonecategory.Items.IndexOf(ddonecategory.Items.FindByText("Your value"));
关键是Items.FindByText
方法
这样做的好处是,如果找不到该项,则将其设置为-1
因此,如果在下拉列表中找不到匹配项,则不会出现任何错误。
这是一个很好的解释
Best way to check if a drop down list contains a value?
这是VB代码
Dim searchString As String = "Toolings".Text
If ddonecategory.Items.FindByText(searchString) IsNot Nothing Then
Label1.Text = "Item Found: " & searchString
Else
Label1.Text = "Item not Found: " & searchString
End If