Please any one help me
how can i add multiple values from one listbox to another on button click[image is attached]
After adding first listbox items must not be visible.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Dec 12, 2013, 12:41 PM
using System;
using System.Collections;
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;
namespace WebApplication9
{
public partial class _Default : System.Web.UI.Page
{
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
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)
{
lbltxt.Visible = false;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (!arraylist2.Contains(ListBox2.Items[i]))
{
arraylist2.Add(ListBox2.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
{
ListBox1.Items.Add(((ListItem)arraylist2[i]));
}
ListBox2.Items.Remove(((ListItem)arraylist2[i]));
}
ListBox1.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox2 to move";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox1.Items[i]))
{
arraylist1.Add(ListBox1.Items[i]);
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox1.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox1 to move";
}
}
}
}
samantaPosted Dec 12, 2013, 10:52 PM
thanks for your help.
samantaPosted Dec 12, 2013, 9:37 AM
one foe add and another for remove
Satyapriya NayakPosted Dec 12, 2013, 9:25 AM
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();
}
}