.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Iterate Listbox item

Posted By:shankey       Posted Date: August 18, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

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
 

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>



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend