How to format individual DropDownlist Items (color, etc.) during onDataBinding event

Posted by LesterDove on Stack Overflow See other posts from Stack Overflow or by LesterDove
Published on 2010-04-09T16:57:29Z Indexed on 2010/04/09 17:13 UTC
Read the original article Hit count: 260

Filed under:
|
|

Hi, 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 OnDataBinding.

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#