python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
add remove items from one listbox to another

Add Remove Items from one ListBox to Another

In this post going to share to code-sample for the Add and Remove items form one ListBox to another ListBox in asp.net with c#.

Some time before, i have needed to add and remove to user role as per your requirements and update the user role in the database. 


Table of Constants

1. In the 1st step, code-sample for aspx page.
2. In the 2nd step, code-sample for C# (.cs page).

In the 1st step, .aspx code-sample



<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <asp:ListBox runat="server" ID="listBoxRoles1"  SelectionMode="Multiple"></asp:ListBox>  
    <asp:Button ID="btn_Add" runat="server" Text="Add" onclick="btn_Add_Click" />
    <asp:Button ID="btn_Remove" runat="server" Text="Remove" onclick="btn_Remove_Click"/>
     <asp:ListBox runat="server" ID="listBoxRoles2" SelectionMode="Multiple"></asp:ListBox>
</asp:Content>

In the 2nd step, .aspx.cs code-sample.


/// <summary>
/// Button used for add items in listBox.
/// </summary>
protected void btn_Add_Click(object sender, EventArgs e)
{
    for (var i = listBoxRoles1.Items.Count - 1; i >= 0; i--)
    {
        if (!listBoxRoles1.Items[i].Selected) continue;
        listBoxRoles2.Items.Add(listBoxRoles1.Items[i]);
        listBoxRoles1.Items.Remove(listBoxRoles1.Items[i]);
    }
}

/// <summary>
/// Button used for remove items in listBox.
/// </summary>
protected void btn_Remove_Click(object sender, EventArgs e)
{
    for (var i = listBoxRoles2.Items.Count - 1; i >= 0; i--)
    {
        if (!listBoxRoles2.Items[i].Selected) continue;
        listBoxRoles1.Items.Add(listBoxRoles2.Items[i]);
        listBoxRoles2.Items.Remove(listBoxRoles2.Items[i]);
    }
}


In the above list-boxes, you can bind the data-source for add and remove items from this ListBox.

ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions