Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • how to display listbox item to be displayed to another listb
AuthorQuestion
how to display listbox item to be displayed to another listb
Posted By narasiman rao on 05 Jan 2013
i have two list box. Design as follows; ListBox1 Button ListBox2 REO Sco Add All Tasco Remove PH2 Remove All when i click the particular item from the ListBox1 that item to be displayed in the ListBox2. similarly when i click Add All Button all items from ListBox1 to be displayed in the ListBox2. when i click the Remove Button that partiuclar item from the listbox2 to be removed. similarly when i click Remove All Button all items from ListBox2 to be Removed. Important one is all item from the ListBox1 is displayed from the database.
Narasiman
AuthorReply
Re: how to display listbox item to be displayed to another listb
Posted By Satyapriya Nayak on 06 Jan 2013  
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:ListBox ID="ListBox1" runat="server" Width="134px" Height="116px"></asp:ListBox>
    <asp:Button ID="Button1" runat="server" Text="&lt;|" onclick="Button1_Click" />
   <asp:Button ID="Button2" runat="server" Text="|&gt;" onclick="Button2_Click" />
   <asp:Button ID="Button3" runat="server" Text="&lt;&lt;|" onclick="Button3_Click" />
   <asp:Button ID="Button4" runat="server" Text="|&gt;&gt;" onclick="Button4_Click" />
    <asp:ListBox ID="ListBox2" runat="server" Width="134px" Height="116px"></asp:ListBox>
    </div>
    </form>
</body>
</html>



using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBox1.Items.Add("Raj");
            ListBox1.Items.Add("Ravi");
            ListBox1.Items.Add("Rahul");
            ListBox1.Items.Add("Raju");
            ListBox1.Items.Add("Rohit");
            ListBox1.Items.Add("Rajesh");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Add(ListBox2.SelectedItem.Text);
        int i = 0;
        i = ListBox2.SelectedIndex;
        ListBox2.Items.RemoveAt(i);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Add(ListBox1.SelectedItem.Text);
        int i = 0;
        i = ListBox1.SelectedIndex;
        ListBox1.Items.RemoveAt(i);

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int j = 0;
        for (j = 0; j <= ListBox2.Items.Count - 1; j++)
        {
            ListBox1.Items.Add(ListBox2.Items[j]);
        }
        ListBox2.Items.Clear();

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int j = 0;
        for (j = 0; j <= ListBox1.Items.Count - 1; j++)
        {
            ListBox2.Items.Add(ListBox1.Items[j]);
        }
        ListBox1.Items.Clear();

    }
}


If this post helps you mark it as answer
Thanks

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.