Bryan
Bryan

Reputation: 8697

Adding a default value into a dropdownlist

I'm trying to add just 1 default value into 1 of my dropdownlistbox.

I've got 2;

  1. DDLocation
  2. DDLpolice

I added items into the DDLocation via the properties. So whenever the user attempts to click one of the items, the DDLpolice will automatically get the value from the database. Unfortunately, there is a persistant bug such as whenever there is only 1 value available, the data will not be displayed. Data will only be displayed if you have 2 values to select before it is able to display the first choice. I bind my DDLocation with the codes below. I added a default value below. Unfortunately the default value only works upon selecting a choice of DDLocation

protected void DDLocation_SelectedIndexChanged(object sender, EventArgs e)
{
        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "Select policeid from LoginRegisterPolice where location='" + lblocation.SelectedItem.Text + "' AND status='available' AND handle='offcase'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);

                DDLpolice.DataSource = ds2;
                DDLpolice.DataTextField = "policeid";
                DDLpolice.DataValueField = "policeid";
                DDLpolice.DataBind();

                DDLpolice.Items.Insert(0, new ListItem("Select Officer", "NA"));
            }

            connAdd.Close();
        }
}

I tried to add a default value into my DDLpolice just like how i did to my previous DDL. Unfortunately, the DDLpolice is being binded, hence there's no option for me to add a default value. I tried using this 2 methods and all failed

I added a Text for this.

<asp:DropDownList ID="DDLpolice" runat="server" DataTextField="dropdownpolice" DataValueField="dropdownpolice" Text="Select Officer" OnSelectedIndexChanged ="DDLpolice_SelectedIndexChanged1" style="height: 22px">
    </asp:DropDownList>

I also tried using this method

protected void DDLpolice_SelectedIndexChanged1(object sender, EventArgs e)
{
    DDLpolice.Items.Add("Select Officer");
}

Hence in conclusion, i reckon i'm not able to add due to the binding of the DDL to the database. Any help would be appreciated.

Regards.


UPDATE! I'm finally able to add a default value by using this method given by AD.net

    <asp:DropDownList ID="DDLpolice" runat="server" DataTextField="dropdownpolice"       DataValueField="dropdownpolice" OnSelectedIndexChanged ="DDLpolice_SelectedIndexChanged1"     style="height: 22px"
    AppendDataBoundItems="true">
    <asp:ListItem Value="">Select Officer</asp:ListItem>
    </asp:DropDownList>

Basically, you would have to add an AppendDataBoundItem method into your source code(html-side). After that, add a ListItemValue which will contain your default value.

Upvotes: 4

Views: 9301

Answers (1)

AD.Net
AD.Net

Reputation: 13409

And that makes sense. Whenever you do DataBind for the DropDownList, it loses any previous values. You can try to add the extra item after the OnDataBound event for the ddl, which should fire after it finishes DataBinding. You can also try playing with this property AppendDataBoundItems and declare a default item in the html.

AppendDataBoundItems let's you append items each time you do databind, so it should not be the right choice for you unless you delete items other than the first one.

You should follow the first approach, insert the default item ondatabound event.

protected void DDLpoliceDataBound(object sender, EventArgs e)
{
    DDLpolice.Items.Insert(0, new ListItem("Select Officer", "NA"));
}

<asp:DropDownList runat="server" ID="DDLpolice" 
ondatabound="DDLpoliceDataBound"></asp:DropDownList>

Upvotes: 2

Related Questions