ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Count and Save number of Click on each Table Row using jQuery Ajax in ASP.Net<p>Hi <a class="username" rel="jovceka">jovceka</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>Here i am using Label to display the count. If you don't want to display the count you can use HiddenField.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;#gvCustomers tr:not(:has(th))&#39;).on(&#39;click&#39;, function () { var count = parseInt($(this).find(&#39;[id*=lblCount]&#39;).html() == &#39;&#39; ? 0 : $(this).find(&#39;[id*=lblCount]&#39;).html()); $(this).find(&#39;[id*=lblCount]&#39;).html(count + 1); }); $(&#39;[id*=btnSave]&#39;).on(&#39;click&#39;, function () { var customers = new Array(); $(&#34;#gvCustomers tr:not(:has(th))&#34;).each(function () { var row = $(this); var customer = {}; customer.Id = parseInt(row.find(&#34;TD&#34;).eq(0).html()); customer.Name = row.find(&#34;TD&#34;).eq(1).html(); customer.Country = row.find(&#34;TD&#34;).eq(2).html(); customer.Count = row.find(&#39;[id*=lblCount]&#39;).html() == &#39;&#39; ? 0 : parseInt(row.find(&#39;[id*=lblCount]&#39;).html()); customers.push(customer); }); $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/InsertCustomers&#34;, data: JSON.stringify({ customers: customers }), contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (r) { alert(&#34;Record(s) inserted.&#34;); } }); }); }); &lt;/script&gt; &lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Count&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblCount&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Save&#34; runat=&#34;server&#34; ID=&#34;btnSave&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); gvCustomers.DataSource = dt; gvCustomers.DataBind(); } } [WebMethod] public static void InsertCustomers(List&lt;Customer&gt; customers) { foreach (Customer customer in customers) { // Insert Code. string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { string query = &#34;INSERT INTO Customers (Id,Name,Country,Count) VALUES (@Id,@Name,@Country,@Count)&#34;; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue(&#34;@Id&#34;, customer.Id); cmd.Parameters.AddWithValue(&#34;@Name&#34;, customer.Name); cmd.Parameters.AddWithValue(&#34;@Country&#34;, customer.Country); cmd.Parameters.AddWithValue(&#34;@Count&#34;, customer.Count); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } } public class Customer { public int Id { get; set; } public string Name { get; set; } public string Country { get; set; } public int Count { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;) dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;) dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;) gvCustomers.DataSource = dt gvCustomers.DataBind() End If End Sub &lt;WebMethod()&gt; Public Shared Sub InsertCustomers(ByVal customers As List(Of Customer)) For Each customer As Customer In customers &#39; Insert Code. Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Dim query As String = &#34;INSERT INTO Customers (Id,Name,Country,Count) VALUES (@Id,@Name,@Country,@Count)&#34; Using cmd As SqlCommand = New SqlCommand(query) cmd.Connection = con cmd.Parameters.AddWithValue(&#34;@Id&#34;, customer.Id) cmd.Parameters.AddWithValue(&#34;@Name&#34;, customer.Name) cmd.Parameters.AddWithValue(&#34;@Country&#34;, customer.Country) cmd.Parameters.AddWithValue(&#34;@Count&#34;, customer.Count) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using Next 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</pre> <p><strong><span style="text-decoration: underline;">Screenshots</span></strong></p> <p><span style="text-decoration: underline;">The Form</span></p> <p><img src="https://i.imgur.com/xHsBICP.gif" alt="" width="298" height="178" /></p> <p><span style="text-decoration: underline;">Values in WebMethod</span></p> <p><span style="text-decoration: underline;"><img src="https://i.imgur.com/BHuLr3K.jpg" alt="" width="472" height="514" /></span></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/125718/Count-and-Save-number-of-Click-on-each-Table-Row-using-jQuery-Ajax-in-ASPNet/https://www.aspforums.net:443/Threads/125718/Count-and-Save-number-of-Click-on-each-Table-Row-using-jQuery-Ajax-in-ASPNet/Mon, 19 Aug 2019 05:29:19 GMT