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.Edit Update ASP.Net GridView cell value on Click using jQuery<p>Hi <a class="username" rel="dorsa">dorsa</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;style type=&#34;text/css&#34;&gt; .hideControl { display: none; } &lt;/style&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://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;[id*=GridView1] tr td&#39;).on(&#39;click&#39;, function () { if ($(this).index() &gt; 1) { $(&#39;[id*=GridView1] tr td&#39;).each(function () { if ($(this).index() &gt; 1) { $(this).find(&#39;span&#39;).show(); $(this).find(&#39;input[type=text]&#39;).hide(); $(this).find(&#39;span&#39;).html($(this).find(&#39;input[type=text]&#39;).val()); } }); var val = $(this).find(&#39;span&#39;).html(); $(this).find(&#39;span&#39;).hide(); $(this).find(&#39;input[type=text]&#39;).val(val); $(this).find(&#39;input[type=text]&#39;).show(); $(this).find(&#39;input[type=text]&#39;).focus(); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; DataKeyNames=&#34;Id&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; ReadOnly=&#34;True&#34; SortExpression=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Title&#34; HeaderText=&#34;Title&#34; SortExpression=&#34;Title&#34; /&gt; &lt;asp:BoundField DataField=&#34;StudentId&#34; HeaderText=&#34;StudentId&#34; SortExpression=&#34;StudentId&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Mark&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lbl_Text1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Mark&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;txt_Text1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Mark&#34;) %&gt;&#39; CssClass=&#34;hideControl&#34; AutoPostBack=&#34;true&#34; OnTextChanged=&#34;Change&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM UpdateTable&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } protected void Change(object sender, EventArgs e) { TextBox txtMark = sender as TextBox; GridViewRow row = (GridViewRow)txtMark.NamingContainer; string query = &#34;UPDATE UpdateTable SET Mark = @Mark WHERE Id = @Id&#34;; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue(&#34;@Mark&#34;, txtMark.Text); cmd.Parameters.AddWithValue(&#34;@Id&#34;, row.Cells[0].Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } BindGrid(); }</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 BindGrid() End If End Sub Private Sub BindGrid() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM UpdateTable&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() End Using End Using End Using End Sub Protected Sub Change(ByVal sender As Object, ByVal e As EventArgs) Dim txtMark As TextBox = TryCast(sender, TextBox) Dim row As GridViewRow = CType(txtMark.NamingContainer, GridViewRow) Dim query As String = &#34;UPDATE UpdateTable SET Mark = @Mark WHERE Id = @Id&#34; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(query, con) cmd.Parameters.AddWithValue(&#34;@Mark&#34;, txtMark.Text) cmd.Parameters.AddWithValue(&#34;@Id&#34;, row.Cells(0).Text) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using BindGrid() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/aKgfVyL.gif" alt="" width="333" height="269" /></p>https://www.aspforums.net:443/Threads/438701/Edit-Update-ASPNet-GridView-cell-value-on-Click-using-jQuery/https://www.aspforums.net:443/Threads/438701/Edit-Update-ASPNet-GridView-cell-value-on-Click-using-jQuery/Mon, 10 Jun 2019 03:15:00 GMT