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.
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".
I do not think Ryan misunderstood. You may have overlooked the part that he is referring to a
Databound DropDownList.
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.
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.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
HOW TO: Add a default ListItem to a DropDownList
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" />
</asp:DropDownList>
dropdownlist Add default ListItem to DropDownList
smartkhilit
Member
6 Points
3 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Aug 10, 2007 04:50 PM|LINK
Hello,
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>
</asp:DropDownList>
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
mbanavige
All-Star
134971 Points
15423 Posts
ASPInsiders
Moderator
MVP
Re: HOW TO: Add a default ListItem to a DropDownList
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 Databound DropDownList.
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.
sneaker
Member
238 Points
50 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Jan 15, 2008 07:30 AM|LINK
A good plan, violent executed now, is better than perfect plan next week
gagan_techie
Member
2 Points
6 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
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.
S.p
Member
8 Points
10 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Apr 02, 2008 07:15 AM|LINK
Hi
We can also add default list item as below:
Dropdownlist1.Items.Add("0",new ListItem(0,"--Select--"));
Thanks
S.p
mbanavige
All-Star
134971 Points
15423 Posts
ASPInsiders
Moderator
MVP
Re: HOW TO: Add a default ListItem to a DropDownList
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.
devbrat_ghos...
Member
112 Points
57 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Aug 28, 2008 11:33 AM|LINK
Thanks Ryan.
Your post solved my issue. You also give the reason behind AppendDataBoundItems. For me reason is most important than solution.
Thanks again sir.
Regards
Dev
Dev
Challenges are What make life interesting; overcoming them is what makes life meaningful.
-Joshua J. Marine
leenya
Member
11 Points
30 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Sep 06, 2008 10:15 AM|LINK
That's awesome..
But how can I tell the Required field validator control that if the value==null then the user did not really choose a country? :(
Nemesis116
Contributor
4667 Points
927 Posts
Re: HOW TO: Add a default ListItem to a DropDownList
Oct 12, 2008 10:56 AM|LINK
I don't think you can without going to code behidn.
Visit my blog