Sum of DropDownList Selected Values Inside GridView Using jQuery

Task Description

We have a GridView Control containing the DropDownlist in it's TemplateField Column (ItemTemplate). Each DropDownList has some selected integer value. The sum of these selected values should display below DropDownList and should also be updated as the user changes any DropDownList selected value.

How to do it

This is done using jQuery. A class "items" is added to DropDownList. This class is further used by jQuery to find the DropDownList on the client-side. On the "document ready" and "DropDownList's change" events a method "CalculateTotal" is called, that performs the main task. Here is the code.

Code Behind

using System;

using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)


        List<Number> list = new List<Number>();

        list.Add(new Number("1"));

        list.Add(new Number("2"));

        list.Add(new Number("3"));

        list.Add(new Number("4"));

        GridView1.DataSource = list;



    public class Number


        public string Num { get; set; }

        public Number(string str)


            this.Num = str;





<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">


<asp:TemplateField HeaderText="Serial Number">


<%# ((Number)Container.DataItem).Num %>



<asp:TemplateField HeaderText="Values">


<asp:DropDownList ID="DropDownList1" runat="server"ClientIDMode="Static" CssClass="items">










<br />

Total : <asp:Label ID="LabelTotal" runat="server" ClientIDMode="Static"></asp:Label>



    $(document).ready(function () {


        $('.items').change(function () { CalculateTotal(); });


    function CalculateTotal() {

        var val = 0;

        $('.items').each(function () {

            val = val + parseFloat($(this).find('option:selected').val());



        val = 0;




DropDownList selected Values