HOW TO: Add a default ListItem to a DropDownList

Rate It (3)

Last post 10-09-2009 11:48 AM by willie55. 15 replies.

Sort Posts:

  • HOW TO: Add a default ListItem to a DropDownList

    08-04-2007, 12:34 PM
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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>

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

    08-10-2007, 12:50 PM
    • Member
      6 point Member
    • smartkhilit
    • Member since 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
    • All-Star
      98,232 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,355
    • 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
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: HOW TO: Add a default ListItem to a DropDownList

    01-15-2008, 3:30 AM
    • Member
      238 point Member
    • sneaker
    • Member since 09-04-2006, 1:50 PM
    • ph
    • Posts 50
    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-08-2008, 2:50 AM
    • Member
      2 point Member
    • gagan_techie
    • Member since 11-02-2007, 9:45 AM
    • Posts 6

    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
    • Member
      8 point Member
    • S.p
    • Member since 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
    • All-Star
      98,232 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,355
    • 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
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: HOW TO: Add a default ListItem to a DropDownList

    08-28-2008, 7:33 AM
    • Member
      105 point Member
    • devbrat_ghosh
    • Member since 05-11-2007, 4:40 AM
    • New Delhi, India
    • Posts 46

    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

    with regards
    Dev

    I never lose, some people are just better than me at winning.
  • Re: HOW TO: Add a default ListItem to a DropDownList

    09-06-2008, 6:15 AM
    • Member
      11 point Member
    • leenya
    • Member since 08-06-2008, 6:00 PM
    • Posts 30

    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? :(

     

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

    10-12-2008, 6:56 AM
    • Contributor
      4,611 point Contributor
    • Nemesis116
    • Member since 09-25-2008, 6:09 AM
    • Geel
    • Posts 919

    I don't think you can without going to code behidn.

    Jef Claes
    ~~~~~~~~~~


    Visit my blog
    Let's Twit!
  • Re: HOW TO: Add a default ListItem to a DropDownList

    10-12-2008, 4:21 PM
    • All-Star
      98,232 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,355
    • Moderator
      TrustedFriends-MVPs

    leenya:

    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? :(

    The value from a dropdownlist will never be null.  if no selection is specifically made, then the selectedvalue of the dropdownlist will be the value of the first listitem.

    usully when you want somebody to make a selection, you'd set the value of the first/default item to an empty string.  This matches the InitialValue property of the RequireFieldValidator and therefore validation fails unless the users selects something other than an item with a blank (empty string) value

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.initialvalue.aspx

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: HOW TO: Add a default ListItem to a DropDownList

    01-09-2009, 8:30 PM
    • Member
      22 point Member
    • taboadar
    • Member since 12-09-2008, 2:34 AM
    • Cornelius
    • Posts 88

    I have a question about that. I have a dropdown list control in one of my pages which get its items from a data source - a table. Also I have a gridview control which displays the results for the selected list item. I want to add  a new item to this list, the "all" items so I will be able to display altogether in the same gridview. Thank you.

    Roberto

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

    01-15-2009, 1:29 AM
    • Contributor
      7,291 point Contributor
    • sirdneo
    • Member since 12-16-2008, 5:45 AM
    • Karachi, Pakistan
    • Posts 1,147

    You can use list.Items.Insert(0,"- ALL -"); after databind.

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

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

    01-15-2009, 3:54 PM
    • Member
      70 point Member
    • jstrosch
    • Member since 04-25-2008, 7:32 PM
    • Posts 15

    sirdneo:

    You can use list.Items.Insert(0,"- ALL -"); after databind.

     

    I've used this exact way to add a 'default' item to a drop down list but when i insert this item it seems to insert it as a new list item at the end of the items list and then swap it with the list item at index zero.  Typically this isn't a problem except when I want the ddl to be sorted alphabetically/etc., anyone found a way to fix this?


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

    01-15-2009, 8:43 PM
    • Member
      22 point Member
    • taboadar
    • Member since 12-09-2008, 2:34 AM
    • Cornelius
    • Posts 88

    yes, but I am not using dinamic connection or code. I am looking all over but I am not find the answer.

     Roberto

     

Page 1 of 2 (16 items) 1 2 Next >