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 ASP.Net GridView cell on Click with DropdownList value using jQuery in C# and VB.Net<p>Hi&nbsp;dorsa,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <p><strong>C#</strong></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;select&#39;).hide(); $(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;select&#39;).show(); $(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(); } }); $(&#34;[id*=DropDownList1]&#34;).on(&#39;change&#39;, function () { var status = $(this).val(); if (status != &#34;&#34;) { $(this).closest(&#39;tr&#39;).find(&#39;[id*=lblStatus]&#39;).html(status &gt;= 1 ? &#34;Present&#34; : &#34;Absent&#34;); $(this).closest(&#39;tr&#39;).find(&#39;[id*=hfStatus]&#39;).val(status); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&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;AttendanceStatus&#34; SortExpression=&#34;AttendanceStatus&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField runat=&#34;server&#34; ID=&#34;hfStatus&#34; Value=&#39;&lt;%# Eval(&#34;AttendanceStatus&#34;) %&gt;&#39; /&gt; &lt;asp:Label ID=&#34;lblStatus&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AttendanceStatus&#34;).ToString() == &#34;False&#34; ? &#34;Absent&#34; : &#34;Present&#34; %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; CssClass=&#34;hideControl&#34;&gt; &lt;asp:ListItem Value=&#34;&#34;&gt;Select&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;0&#34;&gt;Absent&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;1&#34;&gt;Present&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&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;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head id=&#34;Head1&#34; 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;select&#39;).hide(); $(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;select&#39;).show(); $(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(); } }); $(&#34;[id*=DropDownList1]&#34;).on(&#39;change&#39;, function () { var status = $(this).val(); if (status != &#34;&#34;) { $(this).closest(&#39;tr&#39;).find(&#39;[id*=lblStatus]&#39;).html(status &gt;= 1 ? &#34;Present&#34; : &#34;Absent&#34;); $(this).closest(&#39;tr&#39;).find(&#39;[id*=hfStatus]&#39;).val(status); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&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;AttendanceStatus&#34; SortExpression=&#34;AttendanceStatus&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField runat=&#34;server&#34; ID=&#34;hfStatus&#34; Value=&#39;&lt;%# Eval(&#34;AttendanceStatus&#34;) %&gt;&#39; /&gt; &lt;asp:Label ID=&#34;lblStatus&#34; runat=&#34;server&#34; Text=&#39;&lt;%# If(Eval(&#34;AttendanceStatus&#34;).ToString() = &#34;False&#34;, &#34;Absent&#34;, &#34;Present&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; CssClass=&#34;hideControl&#34;&gt; &lt;asp:ListItem Value=&#34;&#34;&gt;Select&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;0&#34;&gt;Absent&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;1&#34;&gt;Present&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&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;/form&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient</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) { 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 status = (row.FindControl(&#34;hfStatus&#34;) as HiddenField).Value.Trim(); string query = &#34;UPDATE UpdateTable SET Mark = @Mark, AttendanceStatus = @Status 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;@Status&#34;, status); 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 status As String = (TryCast(row.FindControl(&#34;hfStatus&#34;), HiddenField)).Value.Trim() Dim query As String = &#34;UPDATE UpdateTable SET Mark = @Mark, AttendanceStatus = @Status 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;@Status&#34;, status) cmd.Parameters.AddWithValue(&#34;@Id&#34;, row.Cells(0).Text) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/2HyEEHM.gif" alt="" width="389" height="163" /></span></strong></p>https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/Tue, 11 Jun 2019 07:24:23 GMTEdit ASP.Net GridView cell on Click with DropdownList value using jQuery in C# and VB.Net<p>Thx dharmendr ,</p> <p>If I want to <span class="tlid-translation translation" lang="en"><span title="">change the place of fields Like below, <span title="" data-original-title=""> I should change which part of the code?!</span> </span></span></p> <p>&nbsp;</p> <pre class="brush: html">&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:TemplateField HeaderText=&#34;AttendanceStatus&#34; SortExpression=&#34;AttendanceStatus&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField runat=&#34;server&#34; ID=&#34;hfStatus&#34; Value=&#39;&lt;%# Eval(&#34;AttendanceStatus&#34;) %&gt;&#39; /&gt; &lt;asp:Label ID=&#34;lblStatus&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AttendanceStatus&#34;).ToString() == &#34;False&#34; ? &#34;Absent&#34; : &#34;Present&#34; %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; CssClass=&#34;hideControl&#34;&gt; &lt;asp:ListItem Value=&#34;&#34;&gt;Select&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;0&#34;&gt;Absent&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;1&#34;&gt;Present&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&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;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;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/Wed, 12 Jun 2019 00:02:32 GMTEdit ASP.Net GridView cell on Click with DropdownList value using jQuery in C# and VB.Net<p>Use this code.</p> <pre class="brush: js">&lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;[id*=GridView1] tr td&#39;).on(&#39;click&#39;, function () { $(&#39;[id*=GridView1] tr td&#39;).each(function () { if ($(this).index() &lt;= 1) { $(this).find(&#39;span&#39;).show(); $(this).find(&#39;select&#39;).hide(); $(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;select&#39;).show(); $(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(); }); $(&#34;[id*=DropDownList1]&#34;).on(&#39;change&#39;, function () { var status = $(this).val(); if (status != &#34;&#34;) { $(this).closest(&#39;tr&#39;).find(&#39;[id*=lblStatus]&#39;).html(status &gt;= 1 ? &#34;Present&#34; : &#34;Absent&#34;); $(this).closest(&#39;tr&#39;).find(&#39;[id*=hfStatus]&#39;).val(status); } }); }); &lt;/script&gt;</pre> <p>In code behind change the cell position to get the Id according to the column index.</p> <pre class="brush: csharp">cmd.Parameters.AddWithValue(&#34;@Id&#34;, row.Cells[0].Text);</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/https://www.aspforums.net:443/Threads/140401/Edit-ASPNet-GridView-cell-on-Click-with-DropdownList-value-using-jQuery-in-C-and-VBNet/Wed, 12 Jun 2019 00:19:24 GMT