如何在 onDataBinding 事件期间格式化单个 DropDownlist 项目(颜色等)
我有一个绑定到 ObjectDataSource 的基本 DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" DataSourceID="objDataSource1"
DataTextField="FieldName" DataValueField="FieldID" />
从中接收 DataTextField 和 DataValueField 值的 DataTable 还返回有关记录的一些其他有趣信息。为了简单起见,请说Active = Y/N
。
我想要做的是根据数据源结果中的 Active 字段设置 DropDownList 项的背景颜色属性。此外,我想在将 DropDownList 绑定到数据时“在同一遍中”执行此操作。所以我的猜测是它必须在 OnDataBound 期间发生。
我已经知道/尝试过的事情:
我可以稍后返回并循环遍历 DropDownList 项目。但它会涉及嵌入循环和重新访问 DataTable 行,而且看起来效率很低
int row; for (行 = 0;行 < DropDownList1.Items.Count - 1;行++) { [[如果该行 = 该数据行]] DropDownList1.Items[行].[[DoStuffHere 等]] }
我们已经通过访问
GridViewRowEventArgOnRowDataBound
事件完成了类似的操作>s e.可以这么说,我似乎缺少的是OnDropDownListItemBound
事件。
希望我说得清楚、简洁。看起来好像应该很容易...
I have a basic DropDownList bound to a ObjectDataSource:
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" DataSourceID="objDataSource1"
DataTextField="FieldName" DataValueField="FieldID" />
The DataTable from which it receives the DataTextField
and DataValueField
values also returns some other interesting information about the records. Say Active = Y/N
for simplicity's sake.
What I'd like to do is to set the background-color property of the DropDownList Item based on that Active field in the DataSource results. Further, I'd like to do this "in the same pass" as when the DropDownList is bound to the data. So my guess is that it has to happen during OnDataBound.
Things I already know/tried:
I could go back and loop through the DropDownList items later. But it would involve embedding loops and re-visiting the DataTable rows and it just seems inefficient
int row; for (row = 0; row < DropDownList1.Items.Count - 1; row++) { [[if this row = that data row]] DropDownList1.Items[row].[[DoStuffHere, etc.]] }
We already do stuff like this with the GridView
OnRowDataBound
event, by accessing theGridViewRowEventArg
s e. What I seem to be missing is anOnDropDownListItemBound
event, so to speak.
Hope I've been clear and concise. Seems as though it should be easy...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在 OnDataBinding 期间执行此操作,因为数据实际上尚未绑定。最好的办法是 (1),即使用 OnDataBound 并循环访问项目。
You can't do it during OnDataBinding because the data has not actually been bound yet. Your best shot is (1), that is, use OnDataBound and loop through the items.
我正在使用这段代码,它对我来说工作得很好:
I am using This code and it's working fine with me: