如何在 onDataBinding 事件期间格式化单个 DropDownlist 项目(颜色等)

发布于 2024-08-28 16:10:22 字数 934 浏览 3 评论 0原文

我有一个绑定到 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 期间发生。

我已经知道/尝试过的事情:

  1. 我可以稍后返回并循环遍历 DropDownList 项目。但它会涉及嵌入循环和重新访问 DataTable 行,而且看起来效率很低

     int row;
     for (行 = 0;行 < DropDownList1.Items.Count - 1;行++)
     {
        [[如果该行 = 该数据行]]
         DropDownList1.Items[行].[[DoStuffHere 等]]
     }
    
  2. 我们已经通过访问 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:

  1. 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.]]
     }
    
  2. We already do stuff like this with the GridView OnRowDataBound event, by accessing the GridViewRowEventArgs e. What I seem to be missing is an OnDropDownListItemBound event, so to speak.

Hope I've been clear and concise. Seems as though it should be easy...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-09-04 16:10:23

您不能在 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");
    }
}

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.

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");
    }
}
你在我安 2024-09-04 16:10:23

我正在使用这段代码,它对我来说工作得很好:

DropDownList1.Items(0).Attributes.CssStyle.Add("color", "Blue");
DropDownList1.Items(0).Attributes.CssStyle.Add("background-color", "#eae9e9");

I am using This code and it's working fine with me:

DropDownList1.Items(0).Attributes.CssStyle.Add("color", "Blue");
DropDownList1.Items(0).Attributes.CssStyle.Add("background-color", "#eae9e9");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文