Adding a blank selection item to the list in drop down list
my dropdownlist is populated from the database as in:
DataTable dt = GetData(); ddlMylist.DataSource = dt; ddlMylist.DataBind();
Now dt contains the data, and I wish to add "Select" word to the top of the list when the selection is blank. It seems as there is no option other than add it to the dt (DataTable object)....but it seems wrong somehow.
What are other options. It's doesn't have to be the word "Select" it can be just an empty space...Currently, upon page load I can see the first data item in the list which is all good and dandy, but I have 3 drop downs that a开发者_JS百科re co-dependent, and I want the user to explicitly make a selection which will trigger other drop downs to populate accordingly.
Try:
ddlMylist.Items.Insert(0, new ListItem([key], [text]));
ddlMylist.SelectedIndex = 0;
You do this after you databind to your source.
Your dropdownlist markup should look like this:
<asp:DropDownList ID="ddlMylist" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="-Select-" Value="" />
</asp:DropDownList>
Note the AppendDataBoundItems
attribute.
Try this one:
ddlMylist.Items.Insert(0, "Select");
Try:
DDL.Text = string.Empty;
Edit:
I know this works when I'm manually adding items but I'm not sure if it will when a DataSource is bound.
精彩评论