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.ASP.Net GridView RowCommand event not working with jQuery DataTable plugin<p>Hey <a class="username" rel="iammann"> iammann</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div class=&#34;container&#34;&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ClientIDMode=&#34;Static&#34; OnRowCommand=&#34;GridView1_RowCommand&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;ID&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;CustomerID&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;ContactName&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Region&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Country&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Select&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;LinkButton1&#34; Text=&#34;Select&#34; runat=&#34;server&#34; CommandName=&#34;Select&#34; CommandArgument=&#34;&lt;%# Container.DataItemIndex %&gt;&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; No Record Available &lt;/EmptyDataTemplate&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css&#34; /&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css&#34; /&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(document).ready(function () { $(&#39;#GridView1&#39;).prepend($(&#34;&lt;thead&gt;&lt;/thead&gt;&#34;).append($(this).find(&#34;tr:first&#34;))).DataTable({ dom: &#39;Bfrtip&#39;, &#39;aoColumnDefs&#39;: [{ &#39;bSortable&#39;: false, &#39;aTargets&#39;: [0]}], &#39;iDisplayLength&#39;: 5, columnDefs: [{ targets: -1, visible: false}] }); }); &lt;/script&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data.SqlClient; using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient</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 (!IsPostBack) { DataTable dt = GetData(); GridView1.DataSource = dt; GridView1.DataBind(); } } private static DataTable GetData() { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;SELECT TOP 10 CustomerID,ContactName,Country FROM Customers&#34;; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int eid = Int32.Parse(e.CommandArgument.ToString()); DataTable dt = GetData(); if (e.CommandName == &#34;Select&#34;) { string customerId = dt.Rows[eid][&#34;CustomerID&#34;].ToString(); ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;alert&#34;, &#34;alert(&#39;CustomerId is : &#34; + customerId + &#34;&#39;);&#34;, true); } }</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 IsPostBack Then Dim dt As DataTable = GetData() GridView1.DataSource = dt GridView1.DataBind() End If End Sub Private Shared Function GetData() As DataTable Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;SELECT TOP 10 CustomerID,ContactName,Country FROM Customers&#34; Dim cmd As SqlCommand = New SqlCommand(query) Using con As SqlConnection = New SqlConnection(conString) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Function Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Dim eid As Integer = Int32.Parse(e.CommandArgument.ToString()) Dim dt As DataTable = GetData() If e.CommandName = &#34;Select&#34; Then Dim customerId As String = dt.Rows(eid)(&#34;CustomerID&#34;).ToString() ScriptManager.RegisterStartupScript(Me, Me.GetType(), &#34;alert&#34;, &#34;alert(&#39;CustomerId is : &#34; &amp; customerId &amp; &#34;&#39;);&#34;, True) End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Li5pFF9.gif" alt="" width="451" height="372" /></p>https://www.aspforums.net:443/Threads/214580/ASPNet-GridView-RowCommand-event-not-working-with-jQuery-DataTable-plugin/https://www.aspforums.net:443/Threads/214580/ASPNet-GridView-RowCommand-event-not-working-with-jQuery-DataTable-plugin/Wed, 26 Dec 2018 07:00:12 GMT