views:

610

answers:

3

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 are co-dependent, and I want the user to explicitly make a selection which will trigger other drop downs to populate accordingly.

A: 

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.

Cory Charlton
doesn't work. Please note that I am using DropDownList not combo box control.After binding this is ignored and before binding, I get an error saying that empty space is not in the data list (which is true)
gnomixa
Ya I didn't see that at first. Sorry to lead you down the wrong path.
Cory Charlton
no worries:) i don't think it's the control actually - it's probably like you said because I use DataBind()
gnomixa
+4  A: 

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.

Joel Coehoorn
Turns out that now, every time the drop down is rebound, the items get appended, which is not what i want. i just want to add "Select" to the top of the list, that's all. This list is dependent on other selection, so its contents should change, not get appended.It looks like it's either appending ALL THE TIME or NEVER with drop down list. very frustrating when such a minute task requires so much work.
gnomixa
You're doing something else wrong with viewstate then to keep these items around.
Joel Coehoorn
no, according to Scott Guthrie's blog:Specifically it is a property called "AppendDataBoundItems", and it controls whether the items within an existing list are replaced or appended-to when the control is databound (with ASP.NET 1.1 the items were always replaced).http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspxsomething to keep in mind
gnomixa
so in other words if you say AppendDataBoundItems - true, the items will always be appended when the list is databound, which is exactly what's happening. Hence I re-opened this question.
gnomixa
Ah, but each postback works with a new instance of the control. Any previous data binding results should be lost. So you're doing something wrong to make these persist in viewstate, even though they shouldn't.
Joel Coehoorn
no, you are incorrect. look at Scott's blog I linked to. I am not touching the viewstate at all, it's the AppendDataBoundItems property, if set to true, results will be APPENDED. Anyways, I solved the issue, so thanks for everyone's help.
gnomixa
I know the items will be appended. Have you _tried_ setting "EnableViewState" to false?
Joel Coehoorn
no, because I don't need to fiddle with it at this point, as I solved the problem. I usually don't touch viewstate unless I have to. In this case the solution I marked as an answer worked fine. Appending the items and disabling view state is more error prone and awkward in this particular case I would rather not append at all.
gnomixa
+1  A: 

Try:

ddlMylist.Items.Insert(0, new ListItem([key], [text]));
ddlMylist.SelectedIndex = 0;

You do this after you databind to your source.

Phil Sandler
for example ... new ListItem( String.Empty, "--Select--")
MikeW