如何使用字符串和值填充DropDownList [英] How Do I Populate A DropDownList with Strings and Values
本文介绍了如何使用字符串和值填充DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Array填充DropDownList,其中包含List,A及其值B中显示的内容。
I am trying to populate a DropDownList with an Array with what is shown in the List, A, and its value, B.
<asp:DropDownList ID="DropDownList1" runat="server" />
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim A(10), B(10) As String
A(0) = "this"
A(1) = "that"
A(2) = "something else"
A(3) = "this else"
A(4) = "that else"
A(5) = "something something else"
B(0) = "1"
B(1) = "2"
B(2) = "3"
B(4) = "4"
B(5) = "5"
'populate DropDownList1 with A
DropDownList1.DataSource = A
DropDownList1.DataTextField = A
DropDownList1.DataValueField = B
DropDownList1.DataBind()
End Sub
推荐答案
您可以填充DropDownList这样。
You can populate a DropDownList like this way.
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim ItemList As IList(Of Object) = New List(Of Object)
ItemList.Add(New With {.A = "this", .B = "1"})
ItemList.Add(New With {.A = "that", .B = "2"})
ItemList.Add(New With {.A = "something else", .B = "3"})
ItemList.Add(New With {.A = "this else", .B = "4"})
ItemList.Add(New With {.A = "something something else", .B = "5"})
'populate DropDownList1 with A
DropDownList1.DataSource = ItemList
DropDownList1.DataTextField = "A"
DropDownList1.DataValueField = "B"
DropDownList1.DataBind()
End Sub
这篇关于如何使用字符串和值填充DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文