Les*_*ove 13 c# asp.net drop-down-menu
我有一个绑定到ObjectDataSource的基本DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" DataSourceID="objDataSource1"
DataTextField="FieldName" DataValueField="FieldID" />
Run Code Online (Sandbox Code Playgroud)
从中接收DataTextField
和DataValueField
值的DataTable 还返回有关记录的一些其他有趣信息.Active = Y/N
为简单起见,请说.
我想要做的是根据DataSource结果中的Active字段设置DropDownList项的background-color属性.此外,我想在DropDownList绑定到数据时"在相同的传递中"执行此操作.所以我的猜测是它必须在OnDataBound期间发生.
我已经知道/尝试的事情:
我可以稍后返回并循环访问DropDownList项.但它会涉及嵌入循环并重新访问DataTable行,这看起来效率低下
int row;
for (row = 0; row < DropDownList1.Items.Count - 1; row++)
{
[[if this row = that data row]]
DropDownList1.Items[row].[[DoStuffHere, etc.]]
}
Run Code Online (Sandbox Code Playgroud)OnRowDataBound
通过访问GridViewRowEventArg
s e ,我们已经通过GridView 事件做了类似这样的事情.我似乎缺少的是一个OnDropDownListItemBound
事件,可以这么说.
希望我清楚简洁.似乎应该很容易......
Mat*_*nes 24
在OnDataBinding期间无法执行此操作,因为数据尚未实际绑定.你最好的镜头是(1),也就是说,使用OnDataBound并遍历项目.
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
foreach(ListItem myItem in DropDownList1.Items)
{
//Do some things to determine the color of the item
//Set the item background-color like so:
myItem.Attributes.Add("style","background-color:#111111");
}
}
Run Code Online (Sandbox Code Playgroud)