问题描述
这是我从来没有解决的问题。我将举例说明了两个代码样本,其中一个工作和其他不:
A question that I have never solved. I will illustrate with two code samples in which one works and the other doesn't:
Page_Load()
{
FontFamily[] oFamilyFontList = FontFamily.Families;
DropDownList_Fonts.DataSource = oFamilyFontList;
DropDownList_Fonts.DataBind();
string[] colorName = System.Enum.GetNames(typeof(KnownColor));
DropDownList_FontColor.DataSource = colorName;
DropDownList_FontColor.DataBind();
}
<asp:DropDownList
ID="DropDownList_Fonts" DataTextField="Name"
DataValueField="Name" runat="server" >
</asp:DropDownList>
<asp:DropDownList
ID="DropDownList_FontColor" DataTextField="colorName"
DataValueField="colorName" runat="server" >
</asp:DropDownList>
第一个DropDownList填充没有任何错误很好,因为每个对象oFamilyFontList具有与结合属性名称在DataText和DataValue领域。
The first DropDownList populates fine without any errors because each object oFamilyFontList has a property 'Name' which binds with the DataText and DataValue fields.
第二个有没有在所有的属性,它只是一个字符串数组。什么可能我可以把两个领域内,使其工作?
The second one has no properties at all and it's just an array of strings. What possibly can I put inside both fields to make it work ?
推荐答案
是的,你可以绑定一个数组,但你必须删除 DataTextField
和 DataValueField
属性
Yes you can bind an array but you have to remove DataTextField
and DataValueField
attributes
<asp:DropDownList
ID="DropDownList_FontColor"
runat="server">
</asp:DropDownList>
这篇关于结合字符串数组为DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!