adding a blank entry into a combo box

  • Thread starter Thread starter Craig G
  • Start date Start date
C

Craig G

i use the following to populate a combo in a datagrid but was wondering how
i would add a blank entry to the top of the combo so that on first load it
has no value selected?

<asp:DropDownList id=cboIntensityAdd runat="server" Width="149px"
CssClass="yarGridCombo" DataValueField="CODE" DataTextField="PAININTENSITY"
DataSource="<%# FillPainIntensityCombo() %>"></asp:DropDownList>

FillPainIntensityCombo simply selects all records from a table using an
oracle stored proc.

Cheers,
Craig
 
After populating the table from the database, add an empty record in the
code.

Eliyahu
 
First of all, I'd really recommend using code-behind classes.

You can't add new item to ddl by setting it's attributes. In your code, type
the following:

ListItem li = new ListItem("Text To Be Displayed","Value");
cboIntensityAdd.Items.Add(li);
 
other responses suggest the right path, but are not your specific solution
which wants an empty item at the top (so that the client can select any OTHER
item successfully, otherwise the initially "selected" item cannot be
immediately re-selected--I think that is your issue!)

1. solution must be in code, either on page or code-behind, as others point
out.
2. after databinding has occurred, you simply insert (not add) an empty
ListItem at the front of the list:

cboIntensityAdd.Insert(0, new ListItem);
 
Back
Top