Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 797
LesterDove Avatar asked Apr 09 '10 16:04

LesterDove


People also ask

How can set the selected value of dropdown in asp net?

FindByValue(stringValue). Selected = true; should work. Save this answer.

How to use DropDownList in c#?

To specify the items that you want to appear in the DropDownList control, place a ListItem object for each entry between the opening and closing tags of the DropDownList control. The DropDownList control also supports data binding. To bind the control to a data source, create a data source, such as a System.

Which of the following will render DropDownList on a web page?

The <option> element is used in conjunction with the <select> element to create a drop-down menu in a web form. Each <option> element is displayed as an available option in the resulting drop-down menu.

How do I add a dropdown in Visual Studio?

Create a Web Form Initially, it is an empty form. Now, we will add a new DropDownList by dragging it from the toolbox. After dragging, our web form looks like the below. Now, to add items to the list, visual studio provides Items property where we can add items.


1 Answers

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");
    }
}
like image 192
Matthew Jones Avatar answered Nov 15 '22 19:11

Matthew Jones