Fill only selected checkbox items from checkboxlist into new list - asp-classic

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#?

Related

How to get Multiple selected listbox items to another listbox

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.

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"));

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.

ASP.NET List Item IDs

The <li> tags generated by ASP.NET when I programatically add ListItems to my ListBox control do not have ID attributes. Is there any way that I can get them to have IDs so that I can get references to the elements from Javascript?
Use the ListItem's attribute collection, like this...
ListItem item = new ListItem();
item.Attributes.Add("id", "myId");
myList.Items.Add(item);

How do I set the selected value in an ASP.NET Drop Down List?

I'm using an ASP.NET dropdown. I want to set the selected element using an ID (Id) which is stored in the DataValue field.
ddlTest.DataValueField = "Id";
ddlTest.DataTextField = "Name";
ddlTest.DataSource = searchResultList;
ddlTest.DataBind();
But all I can see is the SelectedIndex property which isn't the correct thing. How do I set by value?
After it's bound you can set the SelectedValue property to the correct id.
ddlTest.selectedValue = [whatever value you want to set];
If you need the text of the selected item then you can use
dropdwonlistID.SelectedItem.Value // selecting value of the selected item
dropdwonlistID.SelectedItem.Text // selecting displayed text of the selected item

Resources