Digital Colony!

Add Item to DropDownList After DataBind

We often want the first option in a drop-down list to not default to the first item in the list. Instead we would prefer the option of adding a --Select Item-- at the top of the asp:DropDownList. After the DataBind, execute an Item.Insert.
ddlAuthor.DataTextField = "FullName";
ddlAuthor.DataValueField = "AuthorID";
ddlAuthor.DataSource = authorDB.GetAuthors();
ddlAuthor.DataBind();
ddlAuthor.Items.Insert(0, "-- Select Author --");

Labels: ,

AddThis Social Bookmark Button

14 Comments:

Anonymous Anonymous said...

so obvious yet easy to miss. Thanks!

6/26/2007 8:48 PM

Anonymous Anonymous said...

Excellent tip - thanks

8/13/2007 1:19 PM

Anonymous Anonymous said...

Does not work for .net 2.+ version

9/21/2007 3:58 AM

Blogger MAS said...

This code was written for ASP.NET 2.0. Perhaps you copied my DataSource when you tried to run the code? Substitute your own and it should work.

9/21/2007 5:42 AM

Blogger Saurabh said...

DataTable dt = objAdmin.GetPostageTypeAmount(Session["UserCurrency"].ToString(), Session["UserCurrencySymbol"].ToString());
ddlShippingMethod.DataSource = dt.DefaultView;
ddlShippingMethod.DataTextField = "ShippingMethod";
ddlShippingMethod.DataValueField = "ShipingCharge";
ddlShippingMethod.DataBind();
ddlShippingMethod.Items.Insert(0, new ListItem("--Select--", "0"));

I m using this code in a child page. But the 'Select' Item is not being added to the dropdown

2/07/2008 11:47 PM

Blogger MAS said...

Saurabh - I could not replicate your problem. Could there be something wrong with your objAdmin?

Here is the test code I used using your Insert line.

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("AuthorID"));
dt.Columns.Add(new DataColumn("FullName"));

DataRow dr = dt.NewRow();
dr[0] = "10";
dr[1] = "Joe";
dt.Rows.Add(dr);
ddlAuthor.DataSource = dt;
ddlAuthor.DataTextField = "FullName";
ddlAuthor.DataValueField = "AuthorID";
ddlAuthor.DataBind();
ddlAuthor.Items.Insert(0, new ListItem("--Select--", "0"));

2/08/2008 7:27 AM

Blogger Saurabh said...

Thnx Mas. the problem was not in the objAdmin. The list was being populated by the Datatable. Only the Insert after Databind was not working. Actually the list was binding again somewhere in the code n that was creating the problem

i hav added
ddlShippingMethod.DataSource = null;

just after the Databind and its working

2/08/2008 8:20 PM

Blogger David said...

Thanks, really useful!

2/24/2008 2:34 PM

Anonymous Anonymous said...

Thanks, it's very useful

3/29/2008 10:21 PM

Anonymous Anonymous said...

Thanks that was really helpful.

7/15/2008 5:50 AM

Anonymous Anonymous said...

Thanks a lot..that solved my problem

7/16/2008 1:49 AM

Blogger hamy said...

This is good, but I have found that it will cause the SelectedIndexChanged event to fire.

Does anyone know of a way to avoid this occurring? I tried immediately setting it back to

ddl.SelectedIndex = -1, but for some reason walking through the code shows that the command is executed, but the index is not changed (possibly because -1 is not in range anymore?)

Thanks,
Ham

7/24/2008 9:19 AM

Blogger hamy said...

Ah - I have figured out a quick fix to Added the item causing the SelectedIndexChanged event to fire.

Posted about it here (Sorry, I cannot figure out how to do trackbacks using blogger)

http://techrookies.blogspot.com/2008/07/managing-databound-dropdownlists.html

Thanks,
Hamy

7/24/2008 9:47 AM

Blogger Lerma Winchell said...

Thank you so much for sharing this code, this wroked great for my form.

7/29/2008 8:38 AM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.