是否可以将DropDownList控件的属性(DataTextField)设置为显示该项的iten索引:
我需要在DropDownList控件中放置显示每个项目索引的代码,而不是"xxxxxxxx“。有人能帮我吗?
发布于 2011-09-08 00:15:53
我认为在属性«DataTextField»中是不可能的,但您可以利用事件«OnDataBound»在绑定完成后更改每个项目的文本。
我希望这能帮上忙。
发布于 2011-09-08 00:20:30
您可能希望使用KeyValue对列表并将其绑定到您的dropdown控件。值部分( KeyValue pair列表的)可用于显示您希望在下拉列表中显示的任何内容,而键部分( KeyValue pair列表的)可以保存在下拉列表中选择某个项目时要使用的任何值。
发布于 2011-09-08 00:38:49
使用以下方法通过DataBound事件将ListItems Text属性设置为其索引:
标记
<asp:DropDownList ID="dropDownList" runat="server" DataSourceID="sqlDataSource1" OnDataBound="dropDownList_DataBound">
</asp:DropDownList>代码
protected void dropDownList_DataBound(object sender, EventArgs e)
{
int index = 0;
foreach (ListItem item in dropDownList.Items)
{
item.Text = index.ToString();
index++;
}
}https://stackoverflow.com/questions/7337062
复制相似问题