views:

1609

answers:

4

I have a dropdown, I have a datasource, I have AutoPostBack set to true.
I want to add a first entry to the datasource that says something like "--- select country ---" and selecting this entry won't cause postback.
This feels like it should be easy to do, yet I can't seem to be able to find a good solution.
Thanks.

+11  A: 

In your aspx page (the important part is handling the DataBound event and setting CausesValidation="true" to force validation of a drop down list):

<asp:DropDownList ID="ddlCountries" runat="server" DataSourceID="dsCountries" AutoPostBack="true" OnDataBound="ddlCountries_DataBound" CausesValidation="true" />
<asp:RequiredFieldValidator ID="rfvCountries" runat="server" ControlToValidate="ddlCountries" Display="Dynamic" ErrorMessage="Please select a country." />

In your codebehind (it is important that the value of the inserted item is String.Empty for the required field validator to work!):

protected void ddlCountries_DataBound(Object sender, EventArgs e)
{
ddlCountries.Items.Insert(0, new ListItem("--- select country ---", String.Empty));
}

Note: If you don't want the validator's message to display, set the "Display" property to "None".

Daniel Schaffer
+1 This doesn't take care of the desire to not have a postback for just the one item but I'm not sure that any solution outside of extensive Javascript rewiring of the control will do that. I think this is the best simple solution.
Mark Brittingham
I updated my answer to include a validator which should take care of not posting back for the one item.
Daniel Schaffer
+1 (If RequiredFieldValidator doesn't work, just use a CompareValidator)
How can you do that? I know you can compare a value, but we'd want it to fail the comparison to an empty string. Is there a way to do that?
Daniel Schaffer
So I took back the acceptance of this answer. The RequiredFieldValidator displays the error text when I select the "-- select country --" item but causes a postback anyway.
vitule
Ah - you need to add CausesValidation="true" to your dropdownlist to force validation to be respected. Nice catch! I've updated my answer.
Daniel Schaffer
+5  A: 

You can also add the row manually through the designer but you have to make sure that the DropDownList's property AppendDataBoundItems = True as well so that the databound rows are tacked onto the first row.

TheTXI
You have to be careful with this one. Make sure that your don't databind every time page loads, otherwise your dropdown list will get appended to every time the page loads.
nsr81
Good point, I remember having to deal with that early on as well when I first started working with the DDL.
TheTXI
A: 

Previous answers deal with inserting the value, but I understand your problem is the AutoPostBack property. I suppose you dont want to postback that value and that's your problem, am I right?

Maybe there's a better solution, but I'd suggest not using AutoPostBack. You could handle postback automatically using the selected value change event.

IMHO if the AutoPostBack does not work as you want, it's always better to implement your own solution that to put some kind of "patch" over it to "fix" it.

Hope that helps

Rafa G. Argente
thanks. But I feel like this should work out-of-box. With AutoPostBack on, there is no way to select the first entry. So you must insert one that does nothing.
vitule
A: 

Use the insert method as others have suggested to add the item at index 0 with a value to indicate not selected ( for example 'unknown'). Then use validators, add a required field validator and set the InitialValue property to the value of the new list item ('unknown' in our example).

Set index 0 to be the selected item on page load and if not postback.

If the user doesn't select another option the validator will prevent the postback.

Hope that's what you are looking for.