473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Delete Multiple items from Listbox in ASP.NET

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

}

But it's not working.
May 16 '08 #1
7 6149
ShadowLocke
115 100+
If your wanting to remove all the items with this loop you need to replace

Expand|Select|Wrap|Line Numbers
  1. ListBox1.Items.Remove(ListBox1.SelectedValue.ToString());
with
Expand|Select|Wrap|Line Numbers
  1. ListBox1.Items.Remove(ListBox1.Items[i]);
If your only wanting to replace the selected items you need to replace

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i <= ListBox1.Items.Count - 1; i++)
with
Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i <= ListBox1.SelectedItems.Count - 1; i++)
and
Expand|Select|Wrap|Line Numbers
  1. ListBox1.Items.Remove(ListBox1.SelectedValue.ToString());
with
Expand|Select|Wrap|Line Numbers
  1. ListBox1.Items.Remove(ListBox1.SelectedItems[i]);

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

}

But it's not working.
May 16 '08 #2
Hi to remove all the items--
listbox1.items.clear();

Code given by you to remove selected Items is not working.
May 16 '08 #3
Sick0Fant
121 100+
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.

Expand|Select|Wrap|Line Numbers
  1. foreach(object Item in listBox1.SelectedItems)
  2. {
  3.      this.listBox1.Items.Remove(Item);
  4. }
  5.  
  6.  
May 16 '08 #4
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.
May 17 '08 #5
Sick0Fant
121 100+
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:

Expand|Select|Wrap|Line Numbers
  1. while (this.ListBox1.SelectedItem != null )
  2. {
  3.     this.ListBox1.Items.Remove(this.ListBox1.SelectedItem);
  4. }
  5.  
May 19 '08 #6
malav123
217 100+
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....

Expand|Select|Wrap|Line Numbers
  1. if(id=="ctl00_ContentPlaceHolder1_TabAddCompany_TabPanel1_btnnextall")
  2.     {        
  3.         if(lstavailablestate.length < 1) 
  4.         {
  5.               alert('There are no items in the source ListBox');
  6.               return false;
  7.         }
  8.         var i = 0;        
  9.         for (i = 0; i <= lstavailablestate.length -1 ; i++)
  10.         {
  11.             var len = lstassociatedstate.length++;            
  12.             lstassociatedstate.options[len].text = lstavailablestate.options[i].text;                
  13.             lstassociatedstate.options[len].value = lstavailablestate.options[i].value;
  14.             hid = hid + lstavailablestate.options[i].value + ",";
  15.         }       
  16.         for(var i=(lstavailablestate.options.length-1); i>=0; i--)
  17.         {
  18.                 lstavailablestate.options[i] = null;
  19.         } 
  20.  
  21.      }
  22.  
May 22 '08 #7
malav123
217 100+
And ya i have write the code at client side that is in javascript....
May 22 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: theoryboy | last post by:
I'm trying to implement multiple selection functionality in a Tix Hlist using a control-click. I've bound control-click to a function that uses selection_set to add to the selection, but it doesn't...
1
by: Graham Swindon | last post by:
What is the best way to delete multiple records selected from a list box?
3
by: ted | last post by:
Hi, I can't figure out how to set multiple items in a ListBox to selected when the listbox is in a User Control. When the ListBox is set in directly into a form I can use the following:...
0
by: bill yeager | last post by:
Duray, it helps in regards to knowing how to get the items that were selected in the lisbox, but I'm going in reverse --- after I get the items from the db that were selected, I'd like to be able...
2
by: Peter | last post by:
Hello! Please, could anyone tell, is it possible to set multiple items to be selected in list control in the code? For example when the web form is loaded three items of 5 are selected in list...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
1
by: sab | last post by:
Hi, We have a web form with a listbox. The listbox is a multiple select listbox and has data that looks something like: ALL Unit 1 Unit 2 Unit 3 Note: "ALL" is always the first item in...
3
by: Dany P. Wu | last post by:
Hi everyone, One of my Windows forms contain two listbox controls, with Add and Remove buttons between them. The idea is to allow users to select multiple items from one ListBox, click the...
2
by: jim Bob | last post by:
Hi, I have a form with a list box that shows the contents of a table and managed to create an add record button with the wizard. (DoCmd.GoToRecord , , acNewRec) Now i want to make a delete...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.