To Move items from one ListBox to another Listbox
The aspx file code:-
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="width: 100px; height: 104px">
<asp:ListBox ID="lstReturnFields" runat="server" DataTextField="name" DataValueField="start" Rows="6" SelectionMode="Multiple" Width="150px">
</asp:ListBox>
</td>
<td align="center" style="height: 104px" valign="middle">
<asp:ImageButton ID="imgbtnMoveTo" runat="server" CausesValidation="False" ImageUrl="~/Images/less.GIF" OnClick="imgbtnMoveTo_Click" TabIndex="2" />
<br />
<br />
<asp:ImageButton ID="imgbtnFrom" runat="server" CausesValidation="False" ImageUrl="~/Images/grater.gif" OnClick="imgbtnFrom_Click" TabIndex="3" />
</td>
<td style="width: 100px; height: 104px">
<asp:ListBox ID="lstSelectedItems" runat="server" DataTextField="name" DataValueField="start" Rows="6" Width="150px">
</asp:ListBox>
</td>
<td style="height: 104px" valign="middle">
<asp:ImageButton ID="imgbtnMoveUp" runat="server" CausesValidation="False" ImageUrl="~/Images/uparrow.GIF" OnClick="imgbtnMoveUp_Click" TabIndex="4" />
<br />
<br />
<asp:ImageButton ID="imgbtnMoveDown" runat="server" CausesValidation="False" ImageUrl="~/Images/downarrow.GIF" OnClick="imgbtnMoveDown_Click" TabIndex="5" />
</td>
<asp:RequiredFieldValidator ID="rfvlstselecteditems" runat="server" ControlToValidate="lstSelectedItems" Display="None" ErrorMessage="Please provide the Return Fields.">*
</asp:RequiredFieldValidator>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
The apsx.cs file code:-
protected void imgbtnMoveTo_Click(object sender, ImageClickEventArgs e)
{
lstSelectedItems.SelectionMode = ListSelectionMode.Multiple;
for (int iCount = 0; iCount < selectedindex =" lstSelectedItems.Items.Count" selectedindex =" 0;"> 1)
{
imgbtnMoveDown.Enabled = true;
imgbtnMoveUp.Enabled = true;
}
else
{
imgbtnMoveDown.Enabled = false;
imgbtnMoveUp.Enabled = false;
}
imgbtnFrom.Enabled = true;
LoadQueryCriteria();
}
protected void imgbtnFrom_Click(object sender, ImageClickEventArgs e)
{
for (int iCount = 0; iCount < selectedindex =" lstSelectedItems.Items.Count" selectedindex =" 0;" enabled =" false;" enabled =" false;" enabled =" false;" enabled =" true;" enabled =" true;" enabled =" true;"> 1)
{
imgbtnMoveDown.Enabled = true;
imgbtnMoveUp.Enabled = true;
}
else
{
imgbtnMoveDown.Enabled = false;
imgbtnMoveUp.Enabled = false;
}
LoadQueryCriteria();
}
protected void imgbtnMoveUp_Click(object sender, ImageClickEventArgs e)
{
ListItem lstMoveUp = new ListItem();
lstSelectedItems.SelectionMode = ListSelectionMode.Single;
int iSelected = 0;
if (lstSelectedItems.SelectedIndex != 0)
{
iSelected = lstSelectedItems.SelectedIndex;
lstMoveUp.Text = lstSelectedItems.SelectedItem.Text;
lstMoveUp.Value = lstSelectedItems.SelectedItem.Value;
lstSelectedItems.Items[iSelected].Text = lstSelectedItems.Items[iSelected - 1].Text;
lstSelectedItems.Items[iSelected].Value = lstSelectedItems.Items[iSelected - 1].Value;
lstSelectedItems.Items[iSelected - 1].Text = lstMoveUp.Text;
lstSelectedItems.Items[iSelected - 1].Value = lstMoveUp.Value;
lstSelectedItems.SelectedIndex = iSelected - 1;
}
LoadQueryCriteria();
}
protected void imgbtnMoveDown_Click(object sender, ImageClickEventArgs e)
{
ListItem lstMoveUp = new ListItem();
lstSelectedItems.SelectionMode = ListSelectionMode.Single;
int iSelected = 0;
if (lstSelectedItems.SelectedIndex != lstSelectedItems.Items.Count - 1)
{
iSelected = lstSelectedItems.SelectedIndex;
lstMoveUp.Text = lstSelectedItems.SelectedItem.Text;
lstMoveUp.Value = lstSelectedItems.SelectedItem.Value;
lstSelectedItems.Items[iSelected].Text = lstSelectedItems.Items[iSelected + 1].Text;
lstSelectedItems.Items[iSelected].Value = lstSelectedItems.Items[iSelected + 1].Value;
lstSelectedItems.Items[iSelected + 1].Text = lstMoveUp.Text;
lstSelectedItems.Items[iSelected + 1].Value = lstMoveUp.Value;
lstSelectedItems.SelectedIndex = iSelected + 1;
}
LoadQueryCriteria();
}
Comments