How to move items between ListBoxes

Platform: ASP.NET| Category: ListBox

<asp:ListBox id='ListBox1' runat='server'>
	<asp:ListItem Value='Faqs'>Faqs</asp:ListItem>
	<asp:ListItem Value='Tips'>Tips</asp:ListItem>
	<asp:ListItem Value='Tricks'>Tricks</asp:ListItem>
	<asp:ListItem Value='Advanced'>Advanced</asp:ListItem>
</asp:ListBox>
<asp:Button id='btnToRight' style='Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 24px'
	runat='server' Text='>'></asp:Button>
<asp:ListBox id='ListBox2' style='Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 16px' runat='server'></asp:ListBox>
<asp:RequiredFieldValidator id='RequiredFieldValidator1' style='Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 120px'
	runat='server' ErrorMessage='Please Select Item' ControlToValidate='ListBox1'></asp:RequiredFieldValidator>

VB.NET


Private Sub btnToRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToRight.Click
	If ListBox1.Items.Count = 0 Then
		Response.Write('No item to move')
	End If
	Dim itemremoved As String = ListBox1.SelectedItem.Text
	ListBox1.Items.Remove(itemremoved)
	ListBox2.Items.Add(itemremoved)
End Sub

C#


private void btnToRight_Click(object sender, System.EventArgs e)
{
	if (ListBox1.Items.Count == 0 ) 
	{
		Response.Write('No item to move');
	}
string itemremoved    = ListBox1.SelectedItem.Text;
ListBox1.Items.Remove(itemremoved);
ListBox2.Items.Add(itemremoved);
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.