All seems to be correct but the page flickers on any button click and why isn't the Ajax working? Please let me know.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<table>
<tr>
<td >
<asp:UpdatePanel ID="first" runat="server" UpdateMode="Always" >
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Width="150px" Height="150px">
<asp:ListItem Text="first" Value="first"></asp:ListItem>
<asp:ListItem Text="second" Value="second"></asp:ListItem>
<asp:ListItem Text="third" Value="third"></asp:ListItem>
<asp:ListItem Text="forth" Value="forth"></asp:ListItem>
<asp:ListItem Text="fifth" Value="fifth"></asp:ListItem>
</asp:ListBox>
<%--</ContentTemplate>
</asp:UpdatePanel>--%>
</td>
<td valign="middle" align="center" style="width:100px">
<%--<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>--%>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/> <br />
<asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/>
<br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
<%--</ContentTemplate>
</asp:UpdatePanel>--%>
</td>
<td>
<%--<asp:UpdatePanel ID="second" runat="server">
<ContentTemplate>--%>
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" Width="150px" Height="150px">
<asp:ListItem Text="sixth" Value="sevent"></asp:ListItem>
<asp:ListItem Text="seventh" Value="seventh"></asp:ListItem>
</asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
code behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void MoveItems(bool isAdd)
{
if (isAdd)// means if you add items to the right box
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox2.ClearSelection();
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}
else // means if you remove items from the right box and add it back to the left box
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox1.ClearSelection();
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}
private void MoveAllItems(bool isAddAll)
{
if (isAddAll)// means if you add ALL items to the right box
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox2.ClearSelection();
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
else // means if you remove ALL items from the right box and add it back to the left box
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox1.ClearSelection();
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
MoveItems(true);// true since we add
}
protected void ButtonRemove_Click(object sender, EventArgs e)
{
MoveItems(false); // false since we remove
}
protected void ButtonAddAll_Click(object sender, EventArgs e)
{
MoveAllItems(true); // true since we add all
}
protected void ButtonRemoveAll_Click(object sender, EventArgs e)
{
MoveAllItems(false); // false means re remove all
}
}