How to get Multiple selected listbox items to another listbox - asp.net

In my ASP.NET application I have two listboxes, say Listbox1 and Listbox2. Listbox1 having some listitems and which is in Multiple Selection Mode. If I Press the transfer button the selected items in Listbox1 should be moved to Listbox2. I have tried it for Single Selection Move and it works fine.
Now I need help for multiselection.
Code for Single Selection
strItemText = lstAvailableItems.SelectedItem.Text
iItemCode = lstAvailableItems.SelectedValue
lstAvailableItems.Items.Remove(New ListItem(strItemText, iItemCode))
lstSelectedItems.Items.Add(New ListItem(strItemText, iItemCode))
Listbox image
If I press the > button single selected item will be moved from Available Items listbox to selected items list box. How to do this for multiple selection?

A combination of my original, and #Arman's.
Dim lstRemoveItem As New List(Of ListItem)
For Each li As ListItem In lstAvailableItems.Items
If li.Selected Then
lstRemoveItem.Add(New ListItem(li.Text, li.Value))
' can't remove from the collection while looping through it
End If
Next
For Each li As ListItem In lstRemoveItem
lstSelectedItems.Items.Add(li) ' add to "selected" items
lstAvailableItems.Items.Remove(li) ' remove from the original available items
Next

Use List. Iterate all items in the listbox and whatever it finds to be selected, add it to the list.
Dim lstRemoveItem As New List(Of ListItem)
For Each _item As ListItem In lstAvailableItems.Items
If _item.Selected Then
lstRemoveItem.Add(_item)
'here, method to add item to the first list.
End If
Next
For Each _item As ListItem In lstRemoveItem
lstSelectedItems.Items.Add(_item)
Next
Not exactly what you want, but you can configure the variables easily.

Related

Fill only selected checkbox items from checkboxlist into new list

How can i set all selected items from an Checkboxlist into a list?
I tried this code but when i run it all checkbox items where added? I only want the selected items
List<string> WeeklyDays = (from l in CheckBoxListWeeklyDays.Items.Cast<ListItem>() select l.Value).ToList();
Your snippet is C#/ASP.NET not vbscript/classic-asp
In your query add a WHERE statement to look at the "Selected" attribute of each ListItem, similar to:
List WeeklyDays = (from l in CheckBoxListWeeklyDays.Items.Cast() where l.Selected select l.Value).ToList();
Taken from: How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

Weird behavior while creation of two Telerik RadComboBox items programmatically

I've two Telerik RadComboBox controls, while adding of their items using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new RadComboBoxItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlTelerik1.Items.Add(item);
ddlTelerik2.Items.Add(item);
}
at runtime, the first combobox has zero items i.e. items are not added to it while items are added to the second one!
I tried the same for ASP.NET DropDownList using the following code
foreach (var gate in Enum.GetNames(typeof(AuthorizedGates)))
{
var item = new ListItem(gate, Convert.ToString((int)Enum.Parse(typeof(AuthorizedGates), gate)));
ddlAspNet1.Items.Add(item);
ddlAspNet2.Items.Add(item);
}
it is working properly and items got added to both of them.
Any ideas what could be the reason behind this weird behavior?
The difference is that the RadComboBox item is a control like the RadComboBox or DropDownList controls, so it's a class that indirectly inherits from WebControl, and can only have one instance on the page. It's not like a ListItem (which inherits from Object).

Place item at specific position in dropdown list

Can we place/keep dropdownlist item at particular position. Say for example I want to bind dropdownlist to datasource and then add new item which is placed at particular position in dropdownlist. How can this be possible.
You can use Insert method.
dropDownList.Items.Insert(index, new ListItem("Default Panel", "0"));

Splitting Titles in DropDown

My application Drop-down consist of lengthy titles, which exceeds the drop-down beyond the page.
Example Title:
Enter your Name Up to 250 Char Enter your Name Up to 250 Char Enter your Name Up to 250 Char Enter your Name Up to 250 Char.
i can split half of the title with sub string().
Is it possible to bring the next half of the title to next line in Drop-down using C#.
I'm not aware of any property of the DropDownList control that allows you to wrap text, but when I've had items in a DropDownList that exceed the width of the control, I add a tooltip that displays the full text of the item when the mouse hovers over it.
You can do this in code-behind after you've populated the DropDownList:
C#
foreach (ListItem item in DropDownList1.Items)
item.Attributes.Add("Title", item.Text);
VB
For Each item As ListItem In DropDownList1.Items
item.Attributes.Add("Title", item.Text)
End For

Change databound Drop Down List programmatically

I have a drop down list that is populated in the page load event from a database table.
The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).
Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview
GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;
Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?
=============================================EDIT
actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct
This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?
To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:
var row = GridView1.SelectedRow;
if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
{
ProjectList.ClearSelection();
ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
}
Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.
HTH.
You can do
ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;
This will actually set it.

Resources