Can we set a static value at the end of the dropdown?
I have two dropdown lists. Country and state. Both lists are populated from the database. In the Drop Down list for states, I want to add "Others" at the end of the list. I tried adding "Others" from the database, but when I use order by name to display the name of states alphabetically in my dropdown, even "Other" gets reordered, but I want to display it at the end of the list.
This is my code how I populate my list
private void FillStateDDl()
{
DataSet ds = new DataSet();
try
{
SqlParameter[] sql = new SqlParameter[2];
sql[0] = new SqlParameter("@Type", 2);
sql[1] = new SqlParameter("@cid", drpCountry.SelectedValue);
ds = gs.DisplayData("sp_FillDropDownList", sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpState.DataTextField = "statename";
drpState.DataValueField = "sid";
drpState.DataSource = ds;
drpState.DataBind();
drpState.Items.Insert(0, new ListItem("--Select State--", "-1"));
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
source to share
You need to make sure the parameter is AppendDataBoundItems
set to true.
Items can then be added after the data source, for example:
ddl.AppendDataBoundItems = true;
ddl.DataSource = CreateDataSource();
ddl.DataTextField="TextField";
ddl.DataValueField="ValueField";
ddl.DataBind();
ddl.Items.Add("Static value here....");
source to share
Yes, you can set a static value using the datasource in the dropdown. You need to follow these steps:
-
Select the dropdown properties and find AppendDataBoundItems and set AppendDataBoundItems = true.
-
After binding the dropdown, add the following line of code ddl.Items.Add ("Others") to vb.net
-
Also order descending
source to share