[RESOLVED] Add a a first default item to a Drop Down List


I fill a the drop down list with a SQL select query, but i want to add a first default item, something like "- Select an option -" or "All" 

What im trying to do is make a dynamic SQL query, something like this:

SELECT * FROM table1 WHERE (value1 = ? AND value2 = ? AND value3 = ? )........ (or "like" instead of "=")

You are gonna have to select those arguments from 3 drop down lists, but i wanna have a default value on the top of the drop down list options and send a "*" as value if this option its selected, so the filter doesn't consider that value

Any Ideas?

This can be accomplished in code behind. Are you using C#.net or VB.net?

Here's some code that you could use (adapt it for your own uses) 

DropDownList ddlDropDown = ((DropDownList)FormView1.FindControl("ddlDropDown"));
ddlDropDown.Items.Add(new ListItem("-- Select Option --","-1"));
ddlDropDown.AppendDataBoundItems = true;


Private Sub Fill_List()
        Dim list_dt As New DataTable
        list_dt = objList.Bind_List() '' Its return data table
        ddl_list.DataSource = list_dt.DefaultView
        ddl_list.DataTextField = "List_Name"
        ddl_list.DataValueField = "List_Id"
        ddl_list.Items.Insert(0, New ListItem("Select List", "-1")) '' This is first line in your dropdown box.

End Sub

In C#:

private void Fill_List()
	DataTable list_dt = new DataTable();
	list_dt = objList.Bind_List();
	//' Its return data table
	ddl_list.DataSource = list_dt.DefaultView;
	ddl_list.DataTextField = "List_Name";
	ddl_list.DataValueField = "List_Id";
	ddl_list.Items.Insert(0, new ListItem("Select List", "-1"));
	//' This is first line in your dropdown box.




This look perfect. Let me check if this work and i let you know.

Its good to see that your problem has been solved.


This is what i do

  1. DropDownList ddl = DropDownList1;  
  2. ddl.Items.Add(new ListItem("-- Select Option --","-1"));  
  3. ddl.AppendDataBoundItems = true;  

Is this correct? I couldnt get the DropDownList from the FindControl, i was getting just nulls

1 thoughts on "[RESOLVED] Add a a first default item to a Drop Down List"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>