Click here to Skip to main content
15,914,074 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
//design html source
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxExercise.aspx.cs" Inherits="DropDownListBoxControl.CheckBoxExercise" %>

<!DOCTYPE html>



<title>

.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 281px;
}
.auto-style3 {
width: 108px;
}
.auto-style6 {
margin-bottom: 0px;
}
.auto-style7 {
width: 108px;
text-align: center;
height: 128px;
}
.auto-style8 {
text-align: left;
}
.auto-style9 {
font-size: x-large;
}
.auto-style10 {
width: 281px;
height: 128px;
}
.auto-style11 {
height: 128px;
}
.auto-style12 {
width: 108px;
font-size: x-large;
}
.auto-style13 {
width: 281px;
font-size: x-large;
}







<asp:Panel ID="Panel1" runat="server" Height="168px" Width="281px">

                                                               
<asp:CheckBoxList ID="lstCourses" runat="server" AutoPostBack="True" Height="40px" Width="260px">



<asp:Button ID="btnAdd" runat="server" CssClass="auto-style6" OnClick="btnAdd_Click" Text=">" />




<asp:Button ID="btnAll" runat="server" OnClick="btnAll_Click" Text=">>" />
<asp:Panel ID="Panel2" runat="server" Height="168px" Width="267px">
<asp:ListBox ID="lstAddItem" runat="server" Height="145px" SelectionMode="Multiple" Width="221px">

    <asp:Label ID="lblDetails" runat="server" CssClass="auto-style9">
    <asp:Button ID="btnRemove" runat="server" CssClass="auto-style9" OnClick="btnRemove_Click" Text="Remove" />
   
<asp:Button ID="btnRemove0" runat="server" CssClass="auto-style9" Text="Clear All" />




What I have tried:

C#
code file..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DropDownListBoxControl
{
    public partial class CheckBoxExercise : System.Web.UI.Page
    {
        List<listitem> courses = new List<listitem>()
        {
            new ListItem {Text="ASP.NET", Value="3000" },
            new ListItem {Text="SQL Server", Value="2000" },
            new ListItem {Text="MVC",Value="1400" }
        };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (var item in courses)
                {
                    lstCourses.Items.Add(item);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lstAddItem.Items.Clear();
            if(lstAddItem.Items.Contains(lstCourses.SelectedItem))
            {
                lblDetails.Text = "Course is Exists...";

            }
            else
            {                        
           lstAddItem.Items.Add(lstCourses.SelectedItem);
                
            }
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            lstAddItem.Items.Clear();
            foreach(var item in courses)
            {
                lstAddItem.Items.Add(item);
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            
            lstAddItem.Items.Remove(lstAddItem.SelectedItem);
            lblDetails.Text = "Course Removed ......";

        }
    }
}
Posted
Updated 22-Oct-17 22:31pm
v2
Comments
Karthik_Mahalingam 23-Oct-17 3:44am    
what is the issue ?
Pankaj hyderabad 23-Oct-17 4:06am    
i want to add selected value from checkbox list into listbox..
in case if i checked one value so that value is added on list box .
but if i want to add selected items into list box ..then it adding only last selected item..
Karthik_Mahalingam 23-Oct-17 4:23am    
check the solution.
Pankaj hyderabad 23-Oct-17 4:08am    
can u give me code for add one and multiple item from checkboxlist into listbox in asp.net

check the corrections

List<ListItem> courses = new List<ListItem>()
       {
           new ListItem {Text="ASP.NET", Value="3000" },
           new ListItem {Text="SQL Server", Value="2000" },
           new ListItem {Text="MVC",Value="1400" }
       };
       protected void Page_Load(object sender, EventArgs e)
       {

           if (!Page.IsPostBack)
           {
               foreach (var item in courses)
               {
                   lstCourses.Items.Add(item);
               }
           }
       }

       protected void btnAdd_Click(object sender, EventArgs e)
       {
           lstAddItem.Items.Clear();
           foreach (ListItem item in lstCourses.Items)
           {
               if (item.Selected)
                   lstAddItem.Items.Add(item);
           }


       }

       protected void btnAll_Click(object sender, EventArgs e)
       {
           lstAddItem.Items.Clear();
           foreach (var item in courses)
           {
               item.Selected = true;
               lstAddItem.Items.Add(item);
           }
           foreach (ListItem item in lstCourses.Items)
               item.Selected = true;

       }

       protected void btnRemove_Click(object sender, EventArgs e)
       {

           lstAddItem.Items.Remove(lstAddItem.SelectedItem);
           lblDetails.Text = "Course Removed ......";

       }
       protected void btnRemoveAll_Click(object sender, EventArgs e)
       {

           lstAddItem.Items.Clear();
           lblDetails.Text = "Items Cleared......";

       }
 
Share this answer
 
If I am correct, what you want is to get a list of selected checkboxes and add some specific attribute of the control to a list-box.

You can try something like this in your add button click event:
C#
foreach (Control control in form1.Controls)
        {
            if (control is CheckBox && control.Checked)
            {
                //add the item to your list.
            }
        }

It is just a rough skeleton of the code, you may have to tweak a bit for it to work.

Have a good day.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900