ListBox – Select All/Select None

This code sample takes another code sample on the site (2 Listboxes – Moving Items From One to Another) and enhances it. Here, we also show how to add ‘Select All/Select None’ functionality for each listbox.

<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 2.0">
	<title>ListBox - Select All/Select None</TITLE>
	<Link REL=STYLESHEET HREF="/basicArial.css" TYPE="text/css">

<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
        If Not Page.IsPostBack Then
            Dim intFirst As Integer
            For intFirst = 1 To 10
                ListBox1.Items.Add("Item " & intFirst)
            Next
        End If
End Sub

Sub button1_click(Source as Object, E as EventArgs)
        Dim li As ListItem
        For Each li In ListBox1.Items
            If li.Selected = True Then
                ListBox2.Items.Add(li.Text)
            End If
        Next

        Dim counter As Integer
        For counter = (ListBox1.Items.Count - 1) To 0 Step -1
            If ListBox1.Items(counter).Selected = True Then
                ListBox1.Items.RemoveAt(counter)
            End If
       Next
End Sub

Sub button2_click(Source as Object, E as EventArgs)
        Dim counter As Integer
        Dim li As ListItem

        For Each li In ListBox2.Items
            If li.Selected = True Then
                ListBox1.Items.Add(li.Text)
            End If
        Next

        For counter = (ListBox2.Items.Count - 1) To 0 Step -1
            If ListBox2.Items(counter).Selected = True Then
                ListBox2.Items.RemoveAt(counter)
            End If
        Next
End Sub
Sub selAll(Source as Object, E as EventArgs)
	Dim liAll As ListItem
	Dim sList as ListBox
	if Source.id="lbSelectAll" then
		sList=ListBox1
	else
		sList=ListBox2
	End If
        	For Each liAll In sList.Items
	liAll.Selected=true
        Next
End Sub
Sub selNone(Source as Object, E as EventArgs)
        	Dim liAll As ListItem
	Dim sList as ListBox
	if Source.id="lbSelectNone" then
		sList=ListBox1
	else
		sList=ListBox2
	End If
        For Each liAll In sList.Items
	liAll.Selected=false
        Next
End Sub

</script>
</head>
<body>
<div align="center"><Form id="form1" runat="server">
<table  cellpadding="2" cellspacing="2" border=0>
	<tr>
		<td align="center" valign="top">
<asp:ListBox id="ListBox1"
width="125px" height="175"
SelectionMode="Multiple" runat="server" /><br>
Select : <asp:LinkButton id="lbSelectAll"
Text="All" onclick="SelAll" runat="server" />
<asp:LinkButton id="lbSelectNone" Text="None" onclick="selNone" runat="server" /></td>
		<td align="center" valign="center">
<asp:Button id="button1"
width="88px" height="26"
Text="Add >>" onclick="button1_click" runat="server" /><br>
<asp:Button id="button2"
Text="<< Remove" onclick="button2_click"
width="88px" height="26" runat="server" />
		</td>
		<td align="center" valign="top">
<asp:ListBox id="Listbox2" width="125px" height="175"
SelectionMode="Multiple" runat="server" /><br>
Select : <asp:LinkButton id="lb2SelectAll"
Text="All" onclick="SelAll" runat="server" />
<asp:LinkButton id="lb2SelectNone" Text="None" onclick="selNone" runat="server" /></td>
	</tr>
</table>

</Form></div>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>