Dynamically adding items in DropDownList

Last post 05-10-2006 12:49 AM by prashant_victory@hotmail.com. 12 replies.

Sort Posts:

  • Dynamically adding items in DropDownList

    05-08-2006, 3:00 AM

    Hi,

    I wanna ask a question about dropdownlist control in ASP.NET 2.0,

    I've a Dropdownlist (d1) and i want add 5 items in dropdownlist on the Page Load through coding.

    I want to use Add and AddRange Both methods but i'dt know how to use these both methods, So can anybody give me a code for this,

     

    Prashant
  • Re: Dynamically adding items in DropDownList

    05-08-2006, 3:36 AM
    • All-Star
      123,406 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,651
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    you can find information in the documentation of the ListItemCollection members: AddRange. The Items property on the DropDownList is of the type ListItemCollection btw so you can use this class members.

    Grz, Kris.

  • Re: Dynamically adding items in DropDownList

    05-08-2006, 3:58 AM
    • Participant
      1,744 point Participant
    • Dyno1979b
    • Member since 04-10-2006, 9:33 AM
    • Romania
    • Posts 347
    Hope this helps:

    DropDownList.Items.Add("First Item")
    DropDownList.Items.Add("Second Item")

    To add items to the DropDownList, along with a Value Field, you can do something like this (the second item is the one that populates the Value:

    ddl2.Items.Add(New ListItem("Item 1", "1"))
    ddl2.Items.Add(New ListItem("Item 2", "2"))

    Links:
    http://aspnet101.com/aspnet101/tutorials.aspx?id=4
    http://authors.aspalliance.com/stevesmith/articles/dotnetlistbox2.asp


    I don't suffer from madness,
    I enjoy it every minute of my life
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 12:04 AM

    Hello to ALL

    Now i understand the concept behind the Add function of dropdownlist

    For add function i'm using this

    dd1.Add(new ListItem("Text","Value")

    dd1.Add(new ListItem("Text1","Value1")

                      AND

    dd1.Add("Text")

    dd1.Add("Text1")

     

    But i still have a problem with AddRange function of DropDownlist

    So can anyone give me a demo code explaining the concept of AddRange methode.

    Prashant
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 1:16 AM
    • All-Star
      123,406 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,651
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    prashant_victory@hotmail.com:

    So can anyone give me a demo code explaining the concept of AddRange methode.

    Hi,

    did you actually take a look at the link I provided in my previous post? It shows with an example how you can create an array of ListItem objects and add this with the AddRange method.

    Grz, Kris.

  • Re: Dynamically adding items in DropDownList

    05-09-2006, 2:20 AM

    Hello XIII

    The link that u've given to me is not about how to adding the item in DropDownList through AddRange Methode.

    The link that u have given to me is usefull with List box control, and one more thing that code uses a for loop but if i have only two items and i want to add that items through AddRange methode in DropDownList.

    The code u given is fisrt adding the items in the ListItem array and then adding that items to the ListItemCollection but in dropdownlist addrange methode not accept a listitemcollection only accepts the Listitem object.

    I'm doing like this but it give me NullRefrence Exception

    Dim arr(10) As ListItem     'If u put arr() in Place of arr() or if i put new keyword it giving me probs.

    arr(0) =

    New ListItem

    arr(0).Text =

    "A"

    arr(0).Value =

    "1"

    arr(1) =

    New ListItem

    arr(1).Text =

    "B"

    arr(1).Value =

    "2"

    cboSearch.Items.AddRange(arr)

     

    OR if u think the code site that u have posted is works with dropdown so please, give me a proper code.

    Please its a request.

    Byeeeeeeeeeeeee and Take Care.

    Prashant
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 2:42 AM
    • Participant
      1,744 point Participant
    • Dyno1979b
    • Member since 04-10-2006, 9:33 AM
    • Romania
    • Posts 347
    I think you should do it this way:

    Dim arr(2) as ListItem
    arr(0) = new ListItem( "A", "1" )
    arr(1) = new ListItem( "B", "2" )

    cboSearch.Items.AddRange(arr)



    Anyway, here's an example extracted from MSDN
    Dim MyListItemCollection As ListItemCollection = New ListItemCollection()

    ' Dimensions an array of ListItems that are used to populate the collection.   
    Dim NewListItemArray(TempDataTable.Count - 1) As System.Web.UI.WebControls.ListItem

    ' Populates the array of ListItems with generic data.
    Dim i As Integer
    For i = 0 To TempDataTable.Count - 1
        NewListItemArray(i) = New ListItem()
        NewListItemArray(i).Text = "Item " + i.ToString()
        NewListItemArray(i).Value = i.ToString()
    Next

    ' Adds an entire array of ListItem objects to the collection.
    MyListItemCollection.AddRange(NewListItemArray)

    Well, if you're writing the code the way it should be I have no idea what could be the problem


    I don't suffer from madness,
    I enjoy it every minute of my life
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 3:39 AM
    • All-Star
      123,406 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,651
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    prashant_victory@hotmail.com:

    Hello XIII

    The link that u've given to me is not about how to adding the item in DropDownList through AddRange Methode.

    The link that u have given to me is usefull with List box control, and one more thing that code uses a for loop but if i have only two items and i want to add that items through AddRange methode in DropDownList.

    The code u given is fisrt adding the items in the ListItem array and then adding that items to the ListItemCollection but in dropdownlist addrange methode not accept a listitemcollection only accepts the Listitem object.

    I'm doing like this but it give me NullRefrence Exception

    Dim arr(10) As ListItem     'If u put arr() in Place of arr() or if i put new keyword it giving me probs.

    arr(0) =

    New ListItem

    arr(0).Text =

    "A"

    arr(0).Value =

    "1"

    arr(1) =

    New ListItem

    arr(1).Text =

    "B"

    arr(1).Value =

    "2"

    cboSearch.Items.AddRange(arr)

     

    OR if u think the code site that u have posted is works with dropdown so please, give me a proper code.

    Hi,

    of course it works. Besides that the ListBox and DropDownList controls share the same base class ListControl. The example indeed used a for loop to fill up the array but you can also do that manually.

    Take a look at this small code snippet I prepared for you:

    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDLAddRange.aspx.cs" Inherits="DDLAddRange" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml" >
    6    <head runat="server">
    7        <title>Untitled Page</title>
    8    </head>
    9    <body>
    10       <form id="form1" runat="server">
    11       <div>
    12           <asp:DropDownList ID="DropDownList1" runat="server">
    13           </asp:DropDownList></div>
    14       </form>
    15   </body>
    16   </html>
    

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11  
    12   public partial class DDLAddRange : System.Web.UI.Page
    13   {
    14       protected void Page_Load(object sender, EventArgs e)
    15       {
    16  
    17           ListItem[] items = new ListItem[3];
    18           items[0] = new ListItem("One", "1");
    19           items[1] = new ListItem("Two", "2");
    20           items[2] = new ListItem("Three", "3");
    21  
    22           DropDownList1.Items.AddRange(items);
    23           DropDownList1.DataBind();
    24  
    25       }
    26   }

     Grz, Kris.
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 4:42 AM

    Hello XIII ,

    The coding that u have posted is working perfectly in C# But i'm using VB as a language to develop ASP.NET application and the code that u have posted not work in VB

    I writing ur code in VB as ,

    Dim a As New ListItem(3)

    Items(0) =

    New ListItem("One", "1")

    Items(1) =

    New ListItem("Two", "2")

    Items(2) =

    New ListItem("Three", "3")

    cboSearch.Items.AddRange(Items)

    cboSearch.DataBind()

    This code gives me a problem of  "Unable to cast object of type 'System.Collections.Specialized.HybridDictionary' to type 'System.Web.UI.WebControls.ListItem[]'."

    So can u do the same in ASP.NET using vb as a language.

    Prashant
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 6:03 AM
    • All-Star
      123,406 point All-Star
    • XIII
    • Member since 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 13,651
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 7:40 AM

    Dim

    items(3) As ListItem

    items(0) =

    New ListItem("One", "1")

    items(1) =

    New ListItem("Two", "2")

    items(2) =

    New ListItem("Three", "3")

    cboSearch.Items.AddRange(items)

    cboSearch.DataBind()

     

    The code that is generated by C# to VB converter is mentioned above But still it giving me the problem of NullRefrence Exception,

    So please can u try it once and please solve the problem please,

    Prashant
  • Re: Dynamically adding items in DropDownList

    05-09-2006, 10:19 AM
    • All-Star
      76,079 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,181
    • TrustedFriends-MVPs

    That's because in VB, arrays are always 1 larger than what is declared in the Dim statement, and the last element is not being initialized.

    Try this:

    Dim items(2) As ListItem
    items(0) = New ListItem("One", "1")
    items(1) = New ListItem("Two", "2")
    items(2) = New ListItem("Three", "3")
    cboSearch.Items.AddRange(items)

    cboSearch.DataBind()

    Or this:


    Dim items As ListItem() = _
    {New ListItem("One", "1"), _
     New ListItem("Two", "2"), _
     New ListItem("Three", "3")}
    cboSearch.Items.AddRange(items)

    cboSearch.DataBind()

    NC...

  • Re: Dynamically adding items in DropDownList

    05-10-2006, 12:49 AM

    HEY NC01 ,

    both the coding works perfectly,

    Your point is correct that array starts counting as 0,1,2....

    Thanks

    Byeeeeeeeee and Take Care

    Prashant
Page 1 of 1 (13 items)