ListBox Control In ASP.Net 4.5

In this post I’ll give to the demo of ListBox Control In ASP.Net 4.5

This post cover the code, which will transfer value from LIST A to LIST B and vice-verse,   you can transfer single value at a time or all at once, so lest see the code..
 


 

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listcontrol.aspx.cs" Inherits="listcontrol" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Transfer Value From One List Box To Another</h1>
        <table>
            <tr>
                <th>
                    <asp:ListBox ID="listA" runat="server">
                    <asp:ListItem>ASP.Net</asp:ListItem>
                    <asp:ListItem>PHP</asp:ListItem>
                    <asp:ListItem>JAVA</asp:ListItem>
                    <asp:ListItem>RUBY</asp:ListItem>
                    <asp:ListItem>PEARL</asp:ListItem>
                    <asp:ListItem>JavaScript</asp:ListItem>
                    </asp:ListBox>
                </th>
                <td><asp:Button ID="listAtolistB" runat="server" Text=">" OnClick="listAtolistB_Click" /></td>
                <td><asp:Button ID="alllistAtolistB" runat="server" Text=">>" OnClick="alllistAtolistB_Click" /></td>
                <td><asp:Button ID="alllistBtolistA" runat="server" Text="<<" OnClick="alllistBtolistA_Click" style="width: 30px" /></td>
                <td><asp:Button ID="listBtolistA" runat="server" Text="<" OnClick="listBtolistA_Click" /></td>
                <th>
                    <asp:ListBox ID="listB" runat="server">
                    </asp:ListBox>
                </th>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

Default.aspx.cs

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

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

    }
    protected void listAtolistB_Click(object sender, EventArgs e)
    {
        string a = listA.SelectedValue.ToString();
        if (a == "" || a == null)
        {
            Response.Write("<script>alert('Please Select A Value To Transfer');</script>");
        }
        else
        {
            listB.Items.Add(a);
            listA.Items.Remove(a);
        }
    }
    protected void alllistAtolistB_Click(object sender, EventArgs e)
    {
        foreach (ListItem ls in listA.Items)
        {
            listB.Items.Add(ls);
        }
        listA.Items.Clear();

    }
    protected void listBtolistA_Click(object sender, EventArgs e)
    {
        string a = listB.SelectedValue.ToString();
        if (a == "" || a == null)
        {
            Response.Write("<script>alert('Please Select A Value To Transfer');</script>");
        }
        else
        {
            listA.Items.Add(a);
            listB.Items.Remove(a);
        }
    }
    protected void alllistBtolistA_Click(object sender, EventArgs e)
    {
        foreach (ListItem ls in listB.Items)
        {
            listA.Items.Add(ls);
        }
        listB.Items.Clear();
    }
}

This code as you can see is very simple, and easy to understand,

Download Demo Work


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar