HOW TO: Add a default ListItem to a DropDownList

Rate It (1)

Last post 04-29-2008 5:52 PM by mbanavige. 12 replies.

Sort Posts:

  • HOW TO: Add a default ListItem to a DropDownList

    08-04-2007, 12:34 PM

    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>

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Add a default ListItem to a DropDownList

    08-10-2007, 12:50 PM
    • Loading...
    • smartkhilit
    • Joined on 08-10-2007, 4:26 PM
    • Pune, India
    • Posts 3

    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.

    Smile smartkhilit, India

     

  • Re: HOW TO: Add a default ListItem to a DropDownList

    08-12-2007, 9:04 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 6,852
    • Moderator
      TrustedFriends-MVPs

    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.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: HOW TO: Add a default ListItem to a DropDownList

    12-03-2007, 9:57 PM
    • Loading...
    • cafeasp
    • Joined on 04-13-2007, 12:18 AM
    • Posts 99

    Very good, I was looking for this code.

    Keep it simple !
    How-to Videos
    www.vsexpress.info
    www.apriendavisualstudio.net
  • Re: HOW TO: Add a default ListItem to a DropDownList

    01-15-2008, 3:30 AM
    • Loading...
    • sneaker
    • Joined on 09-04-2006, 1:50 PM
    • ph
    • Posts 28
    Exactly...Setting AppendDataBoundItems=True Property ensures that your datasource items will be appended without removing the initial item.
    _sneaker_
    A good plan, violent executed now, is better than perfect plan next week
  • Re: HOW TO: Add a default ListItem to a DropDownList

    02-04-2008, 11:06 PM
    • Loading...
    • konary
    • Joined on 02-05-2008, 3:58 AM
    • Posts 1

    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.

    You did fairly well.

    ______________________
    cat breeds

  • Re: HOW TO: Add a default ListItem to a DropDownList

    02-08-2008, 2:50 AM

    I think Ryan is right and has given correct soution to the problem.we can do this in either ways.

  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-02-2008, 3:15 AM
    • Loading...
    • S.p
    • Joined on 01-18-2008, 10:59 AM
    • Posts 10

    Hi

    We can also add default  list item as below:

    Dropdownlist1.Items.Add("0",new ListItem(0,"--Select--"));

    Thanks

    S.p

  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-02-2008, 6:24 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 6,852
    • Moderator
      TrustedFriends-MVPs

    S.p:

    Dropdownlist1.Items.Add("0",new ListItem(0,"--Select--"));

    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.

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-09-2008, 3:22 AM
    • Loading...
    • abhi@lintas
    • Joined on 04-08-2008, 6:43 AM
    • Posts 6

    if ( Page.IsPostBack)

    {

       List.Items.Insert(0,"Default Value");

    }

    Abhishek

    Abhishek
  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-24-2008, 3:13 AM
    • Loading...
    • zubinjoshi
    • Joined on 02-23-2008, 7:37 AM
    • Delhi India
    • Posts 58

    thnak you

    that 's great.................

    Mark it Answer if it helps you :)
  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-29-2008, 8:35 AM
    • Loading...
    • hinasaxena
    • Joined on 04-29-2008, 12:24 PM
    • Posts 2

    Big Smile please search microsoft to get the required result

    Filed under:
  • Re: HOW TO: Add a default ListItem to a DropDownList

    04-29-2008, 5:52 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 6,852
    • Moderator
      TrustedFriends-MVPs

    There's really no need to go search anywhere else. For this topic, the correct information is posted up at the top of this FAQ thread.

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (13 items)