Listbox Select All for Multiple/Multiselect

Need a simple way to select all or none of the options?

This code is for ASP.NET!

This function needs to be placed once near the top of the page:

[js]
<script type="text/javascript">
/* jak’s select all code */
function lbselectall(listboxname, all) {
sb = document.getElementById(listboxname);
for (var i = 0; i < sb.options.length; i++) {
sb.options[i].selected = all;
}
}
</script>
[/js]

Then for each ListBox, simply put this snippit of HTML below it and edit ListBox1 to reflect the name of the listbox!

[html]
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, true)">all</a>
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, false)">none</a>
[/html]

For a full example, look at this code:

[html]
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>one</asp:ListItem>
<asp:ListItem>two</asp:ListItem>
<asp:ListItem>three</asp:ListItem>
<asp:ListItem>four</asp:ListItem>
</asp:ListBox><br />
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, true)">all</a>
<a href="javascript:lbselectall(‘<%=ListBox1.ClientID %>’, false)">none</a>
[/html]

Hope that help