Thursday, November 8, 2007

Adding a value to a dropdownlist after *.DataBind()

If you have a DropDownList component that is getting fed via a query or an ObjectDataSource, you can insert items into the list in such a way:

DropDownList.DataBind()
DropDownList.Items.Insert(<listLocation>, New ListItem("<name>", <value>))

Where <listLocation> is an integer that tells where to put the item within the DropDownList, <name> being the name that's displayed within the DropDownList, and <value> being the value associated with that item. Example:

DropDownList.DataBind()
DropDownList.Items.Insert(0, New ListItem("Select All", 0))

No comments: