This code will help you to how to iterate and use selected item from listbox.
This code will sum all the selected items from the listbox and display the result in a label
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
<script language="javascript" type="text/javascript">
function loop() {
var sum = 0;
var list = document.getElementById('<%=ListBox1.ClientID %>');
for (var i = 0; i < list.options.length; ++i) {
if (list.options[i].selected == true) {
sum = sum + Number(list.options[i].value);
}
}
document.getElementById('<%=lblTotal.ClientID %>').innerHTML = sum;
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" onclick="javascript:loop();">
<asp:ListItem Value="1">1asp:ListItem>
<asp:ListItem Value="2">2asp:ListItem>
<asp:ListItem Value="3">3asp:ListItem>
<asp:ListItem Value="4">4asp:ListItem>
<asp:ListItem Value="5">5asp:ListItem>
<asp:ListItem Value="6">6asp:ListItem>
asp:ListBox>
div>
Sum is : <asp:Label ID="lblTotal" runat="server">asp:Label>
form>
body>
html>