Delete Multiple items from Listbox in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonia.sardana
    New Member
    • Jul 2006
    • 95

    Delete Multiple items from Listbox in ASP.NET

    I try the foll. code---
    protected void Button1_Click(o bject sender, EventArgs e)
    {
    int i;
    for (i = 0; i <= ListBox1.Items. Count - 1; i++)
    {
    ListBox1.Items. Remove(ListBox1 .SelectedValue. ToString());
    }

    }

    But it's not working.
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    #2
    If your wanting to remove all the items with this loop you need to replace

    Code:
    ListBox1.Items.Remove(ListBox1.SelectedValue.ToString());
    with
    Code:
    ListBox1.Items.Remove(ListBox1.Items[i]);
    If your only wanting to replace the selected items you need to replace

    Code:
    for (i = 0; i <= ListBox1.Items.Count - 1; i++)
    with
    Code:
    for (i = 0; i <= ListBox1.SelectedItems.Count - 1; i++)
    and
    Code:
    ListBox1.Items.Remove(ListBox1.SelectedValue.ToString());
    with
    Code:
    ListBox1.Items.Remove(ListBox1.SelectedItems[i]);

    Originally posted by sonia.sardana
    I try the foll. code---
    protected void Button1_Click(o bject sender, EventArgs e)
    {
    int i;
    for (i = 0; i <= ListBox1.Items. Count - 1; i++)
    {
    ListBox1.Items. Remove(ListBox1 .SelectedValue. ToString());
    }

    }

    But it's not working.
    Last edited by ShadowLocke; May 16 '08, 08:50 PM. Reason: Too many )

    Comment

    • sonia.sardana
      New Member
      • Jul 2006
      • 95

      #3
      Hi to remove all the items--
      listbox1.items. clear();

      Code given by you to remove selected Items is not working.

      Comment

      • Sick0Fant
        New Member
        • Feb 2008
        • 121

        #4
        Are you trying to delete the selected items? If so, check out the SelectedItems property.

        Also, since you are deleting on each iteration, you cannot go forward, since deleting from the front makes all the items indeces decrease. To fix that, either iterate for the number of items, deleting the 0th element on each iteration, or else start from the back and decrement, each time deleting the ith element.

        You also might want to use the foreach loop, as it implicitly iterates until the collection is out of elements.

        Code:
        foreach(object Item in listBox1.SelectedItems)
        {
             this.listBox1.Items.Remove(Item);
        }

        Comment

        • sonia.sardana
          New Member
          • Jul 2006
          • 95

          #5
          hey frnds, there is no SelectedItems Property in ASP.Net, y al u r replying SelectedItems Property,IN ASP there is either SelectedItem ya SelectedValue Property.

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by sonia.sardana
            hey frnds, there is no SelectedItems Property in ASP.Net, y al u r replying SelectedItems Property,IN ASP there is either SelectedItem ya SelectedValue Property.
            Oh, I'm sorry. I forgot that this was ASP. Try:

            Code:
            while (this.ListBox1.SelectedItem != null )
            {
                this.ListBox1.Items.Remove(this.ListBox1.SelectedItem);
            }

            Comment

            • malav123
              New Member
              • Feb 2008
              • 217

              #7
              Hi Sonia,


              Tell exact requirement of your project... if u want to move all the items of listbox then use following code which will allows u to move all the items from one list box to another.. and by editing this code u can also just removes the items of particular listbox....

              Code:
              if(id=="ctl00_ContentPlaceHolder1_TabAddCompany_TabPanel1_btnnextall")
                  {        
                      if(lstavailablestate.length < 1) 
                      {
                            alert('There are no items in the source ListBox');
                            return false;
                      }
                      var i = 0;        
                      for (i = 0; i <= lstavailablestate.length -1 ; i++)
                      {
                          var len = lstassociatedstate.length++;            
                          lstassociatedstate.options[len].text = lstavailablestate.options[i].text;                
                          lstassociatedstate.options[len].value = lstavailablestate.options[i].value;
                          hid = hid + lstavailablestate.options[i].value + ",";
                      }       
                      for(var i=(lstavailablestate.options.length-1); i>=0; i--)
                      {
                              lstavailablestate.options[i] = null;
                      } 
                             
                   }

              Comment

              • malav123
                New Member
                • Feb 2008
                • 217

                #8
                And ya i have write the code at client side that is in javascript....

                Comment

                Working...