Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 16, 2009 12:43 AM by taboadar
Aug 04, 2007 04:34 PM|LINK
This can be done one of 2 ways. A default ListItem can be added to a DropDownList programmatically with the following syntax after binding data to the DropDownList:
//Code here to populate DropDownList
DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")
This will add a ListItem to index 0, which will be the first ListItem.
In .NET 2.0, this can be done declaratively using the
AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections.
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Default text" Value="Default value" />
Add default ListItem to DropDownList
Aug 10, 2007 04:50 PM|LINK
I think you misunderstood a little bit. Well, The Text property means what should be displayed to the user in the dropdownlist, and the value property means - what is the value of that text displayed in the dropdownlist. E.g. Text property could be "SomeText"
and value property of that text can be like 1, or anything. The actual property of dropdownlist is "Selected=True".
So, try this one,
<asp:DropDownList ID="DropDownList1" runat="server" width="145px">
<asp:ListItem Text="SomeText" Value="SomeValue" Selected="true"></asp:ListItem>
You can also cross verify, that Value="Default Value" has no connection with being selected as well as being Default Selected Value at first.
[:)] smartkhilit, India
Aug 12, 2007 01:04 PM|LINK
I do not think Ryan misunderstood. You may have overlooked the part that he is referring to a
When a DropdownList is databound, the DDL's Items collection is cleared by default before the new items are added. This would remove any ListItems you had added in the markup. In the example you have provided, your ListItem (SomeText/SomeValue) will be
lost once the DDL is databound.
To prevent ListItems defined in your markup from being removed when databinding, you would need to set the AppendDataBoundItems to True as Ryan had suggested.
Jan 15, 2008 07:30 AM|LINK
Feb 08, 2008 06:50 AM|LINK
I think Ryan is right and has given correct soution to the problem.we can do this in either ways.
Apr 02, 2008 07:15 AM|LINK
We can also add default list item as below:
Apr 02, 2008 10:24 AM|LINK
The Add method does not support those arguments. As noted in Ryans post, to add an item at position zero of the ListItem collection, you would use the Insert method of the ListItemCollection.
Aug 28, 2008 11:33 AM|LINK
Your post solved my issue. You also give the reason behind AppendDataBoundItems. For me reason is most important than solution.
Thanks again sir.
Sep 06, 2008 10:15 AM|LINK
But how can I tell the Required field validator control that if the value==null then the user did not really choose a country? :(
Oct 12, 2008 10:56 AM|LINK
I don't think you can without going to code behidn.