Add item from Listbox to listbox
I have two list boxes and 2 buttons
when i click on Add button selected listbox1 items get added to listbox2. And
i click on Remove button selected listbox2 items get added to listbox1 and its appended at last but i want items must be added back to same index.
samantaPosted Dec 14, 2013, 12:19 AM
i have tried but i didn't got actual solution.
when i remove items from textbox2 the items must be arranged as earliar in textbox1
Satyapriya NayakPosted Dec 14, 2013, 12:06 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Untitled Page
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();
}