Databound drop down list - initial value

101,605

Solution 1

I think what you want to do is this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="--Select One--" Value="" />   
</asp:DropDownList>

Make sure the 'AppendDataBoundItems' is set to true or else you will clear the '--Select One--' list item when you bind your data.

If you have the 'AutoPostBack' property of the drop down list set to true you will have to also set the 'CausesValidation' property to true then use a 'RequiredFieldValidator' to make sure the '--Select One--' option doesn't cause a postback.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>

Solution 2

I know this is old, but a combination of these ideas leads to a very elegant solution:

Keep all the default property settings for the DropDownList (AppendDataBoundItems=false, Items empty). Then handle the DataBound event like this:

protected void dropdown_DataBound(object sender, EventArgs e)
{
    DropDownList list = sender as DropDownList;
    if (list != null)
        list.Items.Insert(0, "--Select One--");
}

The icing on the cake is that this one handler can be shared by any number of DropDownList objects, or even put into a general-purpose utility library for all your projects.

Solution 3

What I do is set the text property of the drop down list AFTER I databind it. Something like this:

   protected void LoadPersonComboBox()
    {
        var p = new PeopleBLL();

        rcmbboxEditPerson.DataSource = p.GetPeople();
        rcmbboxEditPerson.DataBind();
        rcmbboxEditPerson.Text = "Please select an existing person to edit...";
    }

This makes the initial visible value of this dropdown show up, but not actually be a part of the drop down, nor is it a selectable.

Solution 4

I know this already has a chosen answer - but I wanted to toss in my two cents. I have a databound dropdown list:

<asp:DropDownList 
  id="country" 
  runat="server" 
  CssClass="selectOne" 
  DataSourceID="country_code" 
  DataTextField="Name" 
  DataValueField="CountryCode_PK"
></asp:DropDownList>
<asp:SqlDataSource 
  id="country_code" 
  runat="server" 
  ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
  SelectCommand="SELECT CountryCode_PK, CountryCode_PK + ' - ' + Name AS N'Name' FROM TBL_Country ORDER BY CountryCode_PK"
></asp:SqlDataSource>

In the codebehind, I have this - (which selects United States by default):

if (this.IsPostBack)
{
  //handle posted data
}
else 
{
  country.SelectedValue = "US";
}

The page initially loads based on the 'US' value rather than trying to worry about a selectedIndex (what if another item is added into the data table - I don't want to have to re-code)

Solution 5

To select a value from the dropdown use the index like this:

if we have the

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true"></asp:DropDownList>

you would use :

DropDownList1.Items[DropDownList1.SelectedIndex].Value

this would return the value for the selected index.

Share:
101,605
mmcglynn
Author by

mmcglynn

Web Developer from Providence, RI. Doing my best to ask good questions. Hoping for civility and reason when I don't.

Updated on August 31, 2020

Comments

  • mmcglynn
    mmcglynn over 3 years

    How do I set the initial value of a databound drop down list in ASP.NET?

    For instance, I want the values, but the first value to display should be -- Select One ---, with a null value.