Count and Save number of Click on each Table Row using jQuery Ajax in ASP.Net

Last Reply 10 months ago By dharmendr

Posted 10 months ago

how to count the number of clicks on each row in jquery ajax table and increment and save the total count for each row  in database with respect to row id using jquery ajax json webmethod

You are viewing reply posted by: dharmendr 10 months ago.
Posted 10 months ago Modified on 10 months ago

Hi jovceka,

Check this example. Now please take its reference and correct your code.

Here i am using Label to display the count. If you don't want to display the count you can use HiddenField.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
    $(function () {
        $('#gvCustomers tr:not(:has(th))').on('click', function () {
            var count = parseInt($(this).find('[id*=lblCount]').html() == '' ? 0 : $(this).find('[id*=lblCount]').html());
            $(this).find('[id*=lblCount]').html(count + 1);

        $('[id*=btnSave]').on('click', function () {
            var customers = new Array();
            $("#gvCustomers tr:not(:has(th))").each(function () {
                var row = $(this);
                var customer = {};
                customer.Id = parseInt(row.find("TD").eq(0).html());
                customer.Name = row.find("TD").eq(1).html();
                customer.Country = row.find("TD").eq(2).html();
                customer.Count = row.find('[id*=lblCount]').html() == '' ? 0 : parseInt(row.find('[id*=lblCount]').html());
                type: "POST",
                url: "Default.aspx/InsertCustomers",
                data: JSON.stringify({ customers: customers }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert("Record(s) inserted.");
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:TemplateField HeaderText="Count">
                <asp:Label ID="lblCount" runat="server" />
<br />
<asp:Button Text="Save" runat="server" ID="btnSave" />



using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;


Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services



protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        gvCustomers.DataSource = dt;

public static void InsertCustomers(List<Customer> customers)
    foreach (Customer customer in customers)
        // Insert Code.
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
            string query = "INSERT INTO Customers (Id,Name,Country,Count) VALUES (@Id,@Name,@Country,@Count)";
            using (SqlCommand cmd = new SqlCommand(query))
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Id", customer.Id);
                cmd.Parameters.AddWithValue("@Name", customer.Name);
                cmd.Parameters.AddWithValue("@Country", customer.Country);
                cmd.Parameters.AddWithValue("@Count", customer.Count);

public class Customer
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public int Count { get; set; }


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        gvCustomers.DataSource = dt
    End If
End Sub

Public Shared Sub InsertCustomers(ByVal customers As List(Of Customer))
    For Each customer As Customer In customers
        ' Insert Code.
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Dim query As String = "INSERT INTO Customers (Id,Name,Country,Count) VALUES (@Id,@Name,@Country,@Count)"
            Using cmd As SqlCommand = New SqlCommand(query)
                cmd.Connection = con
                cmd.Parameters.AddWithValue("@Id", customer.Id)
                cmd.Parameters.AddWithValue("@Name", customer.Name)
                cmd.Parameters.AddWithValue("@Country", customer.Country)
                cmd.Parameters.AddWithValue("@Count", customer.Count)
            End Using
        End Using
End Sub

Public Class Customer
    Public Property Id As Integer
    Public Property Name As String
    Public Property Country As String
    Public Property Count As Integer
End Class


The Form

Values in WebMethod