connectseekers.com

Tuesday, 20 January 2015

Move ListBox Items From one ListBox To another ListBox Using C#.Net In Asp.Net

1/20/2015 - By Pranav Singh 0



This article will show you how you can move listbox item from one listbox to another listbox using c#.net in asp.net.



So for this article first we will create a new asp.net application and add below code into the page.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="WebApplication1.WebForm6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Move ListBox Items From one ListBox To another ListBox Using C#.Net In Asp.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%">
        <tr>
            <td>
                <asp:ListBox ID="ListBox1" runat="server" Height="105px" Width="118px"
                    SelectionMode="Multiple">
                    <asp:ListItem>List1 Item 1</asp:ListItem>
                    <asp:ListItem>List1 Item2</asp:ListItem>
                    <asp:ListItem>List1 Item 3</asp:ListItem>
                    <asp:ListItem>List1 Item 4</asp:ListItem>
                    <asp:ListItem>List1 Item 5</asp:ListItem>
                </asp:ListBox>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text=">>"
                    onclick="Button1_Click" /><br />
                <asp:Button ID="Button2" runat="server" Text="<<"
                    onclick="Button2_Click" />
            </td>
            <td>
                <asp:ListBox ID="ListBox2" runat="server" Height="105px" Width="118px"
                    SelectionMode="Multiple">
                    <asp:ListItem>List2 Item 1</asp:ListItem>
                    <asp:ListItem>List2 Item2</asp:ListItem>
                    <asp:ListItem>List2 Item 3</asp:ListItem>
                    <asp:ListItem>List2 Item 4</asp:ListItem>
                    <asp:ListItem>List2 Item 5</asp:ListItem>
                </asp:ListBox>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


In above code when we move item from one list box to another listbox post back will take place.
So here is the code to move the one list box item to another listbox.

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

namespace WebApplication1
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                ListBox2.Items.Add(ListBox1.SelectedItem);
                ListBox1.Items.Remove(ListBox1.SelectedItem);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedIndex != -1)
            {
                ListBox1.Items.Add(ListBox2.SelectedItem);
                ListBox2.Items.Remove(ListBox2.SelectedItem);
            }
        }
    }
}

Now we have done run the application and check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top