如何格式化onDataBinding活动期间个人DROPDOWNLIST项目(颜色等) [英] How to format individual DropDownlist Items (color, etc.) during onDataBinding event

查看:205
本文介绍了如何格式化onDataBinding活动期间个人DROPDOWNLIST项目(颜色等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须绑定到一个ObjectDataSource的一个基本的DropDownList:

I have a basic DropDownList bound to a ObjectDataSource:

<asp:DropDownList ID="DropDownList1" runat="server" 
AutoPostBack="True" DataSourceID="objDataSource1" 
DataTextField="FieldName" DataValueField="FieldID" />

从它接收到 DataTextField DataValueField 值的数据表还返回有关记录其他一些有趣的信息。说活动= Y / N 为简单起见。

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.

我想要做的是设置基于数据源的结果有源现场将DropDownList项目的background-color属性。此外,我想这样做在同一个通行证时,将DropDownList绑定到数据。所以我的猜测是,它具有OnDataBound期间发生的。

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. 我以后可以回去遍历DropDownList的项目。但它会涉及嵌入循环和再逛数据表行和它只是似乎效率不高。

  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.]]
 }


  • 我们已经在做这样的东西用在GridView OnRowDataBound 活动,通过访问 GridViewRowEventArg 发E 。我似乎缺少是 OnDropDownListItemBound 事件,可以这么说。

  • 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...

    推荐答案

    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.

    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");
        }
    }
    

    这篇关于如何格式化onDataBinding活动期间个人DROPDOWNLIST项目(颜色等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆