<head>
<title>Setting Multiple Selections in a List Control</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
void setSelected ( object src, EventArgs e ) {
int i = 0;
foreach ( ListItem item in myList.Items ) {
if ( i % 2 == 0 ) item.Selected = true;
i++;
}
getSelected ( myList, null );
}
void getSelected ( object src, EventArgs e ) {
string s = "Selected items:<br>";
foreach ( ListItem item in myList.Items ) {
if ( item.Selected ) {
// list the selected item
s += item.Text + "<br>";
}
}
msg.Text = s;
}
void clearSelected ( object src, EventArgs e ) {
foreach ( ListItem item in myList.Items ) {
item.Selected = false;
}
msg.Text = "No selected item";
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Setting Multiple Selections in a List Control</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<form runat="server">
<asp:ListBox id="myList" selectionmode="multiple" width=100 runat="server">
<asp:listitem value="earth.gif" text="Earth"/>
<asp:listitem value="jupiter.gif" text="Jupiter"/>
<asp:listitem value="mars.gif" text="Mars"/>
<asp:listitem value="mercury.gif" text="Mercury"/>
<asp:listitem value="neptune.gif" text="Neptune"/>
<asp:listitem value="pluto.gif" text="Pluto"/>
<asp:listitem value="saturn.gif" text="Saturn"/>
<asp:listitem value="uranus.gif" text="Uranus"/>
<asp:listitem value="venus.gif" text="Venus"/>
</asp:ListBox>
<asp:button Text="Select" onClick="setSelected" runat="server" />
<asp:button Text="Clear" onClick="clearSelected" runat="server" />
<p><asp:Label id="msg" runat="server" />
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>