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.Populate ASP.Net GridView on another GridView Selected Row using jQuery AJAX and WebMethod<p>Hey <a class="username" rel="Warren"> Warren</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&#34; /&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;[id*=gvtfaTransaction]&#34;).find(&#34;[id*=btnSelect]&#34;).click(function () { var row = $(this).closest(&#34;tr&#34;); var index = row[0].rowIndex - 1; var custid = row.find(&#34;td&#34;).eq(0).html(); $.ajax({ type: &#34;POST&#34;, url: &#34;CS.aspx/GetData&#34;, data: &#34;{custid:&#34; + custid + &#34;}&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, error: function (jqXHR, textStatus, errorThrown) { alert(&#34;get session failed &#34; + errorThrown); }, success: function (response) { $(&#39;#gvtfaDetails&#39;).focus(); document.activeElement.scrollIntoView(true); var xmlDoc = $.parseXML(response.d); var xml = $(xmlDoc); var customers = xml.find(&#34;Table&#34;); var row = $(&#34;[id*=gvtfaDetails] tr:last-child&#34;).clone(true); $(&#34;[id*=gvtfaDetails] tr&#34;).not($(&#34;[id*=gvtfaDetails] tr:first-child&#34;)).remove(); $.each(customers, function () { $(&#34;td&#34;, row).eq(0).html($(this).find(&#34;CustomerId&#34;).text()); $(&#34;[id*=LabelCheckMemo]&#34;, row).html($(this).find(&#34;Name&#34;).text()); $(&#34;[id*=TextCheckMemo]&#34;, row).val($(this).find(&#34;Country&#34;).text()); $(&#34;[id*=gvtfaDetails]&#34;).append(row); row = $(&#34;[id*=gvtfaDetails] tr:last-child&#34;).clone(true); }); } }); return false; }); $(&#39;.Mark&#39;).on(&#39;change&#39;, function () { var id = $(this).closest(&#39;tr&#39;).find(&#39;td&#39;).eq(0).html(); var checkbox = $(this).closest(&#39;tr&#39;).find(&#39;[id*=btnMark]&#39;); var Marked = $(this).closest(&#39;tr&#39;).find(&#39;[id*=btnMark]:checked&#39;).val(); var DropdownlistGLAcct = $(this).closest(&#39;tr&#39;).find(&#39;[id*=DropdownlistGLAcct]&#39;); var LabelGLAcct = $(this).closest(&#39;tr&#39;).find(&#39;[id*=LabelGLAcct]&#39;); var TextExchangeRate = $(this).closest(&#39;tr&#39;).find(&#39;[id*=TextExchangeRate]&#39;); var LabelFullname = document.getElementById(&#39;&lt;%=LabelFullname.ClientID%&gt;&#39;).innerText; $.ajax({ type: &#34;POST&#34;, url: &#34;CS.aspx/Mark&#34;, data: &#34;{id:&#34; + id + &#34;,Marked:&#39;&#34; + Marked + &#34;&#39;, LabelFullname:&#39;&#34; + LabelFullname + &#34;&#39;}&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, error: function (jqXHR, textStatus, errorThrown) { alert(&#34;get session failed &#34; + errorThrown); }, success: function (response) { if (response.d == &#34;failed&#34;) { $(checkbox).prop(&#39;checked&#39;, false); $(DropdownlistGLAcct).prop(&#34;disabled&#34;, false); $(DropdownlistGLAcct).closest(&#39;td&#39;).find(&#39;button&#39;).removeClass(&#39;disabled&#39;); } else { if ($(checkbox).prop(&#39;checked&#39;) == false) { $(TextExchangeRate).prop(&#34;disabled&#34;, false); $(DropdownlistGLAcct).prop(&#34;disabled&#34;, false); $(DropdownlistGLAcct).closest(&#39;td&#39;).find(&#39;button&#39;).removeClass(&#39;disabled&#39;); } else { $(TextExchangeRate).prop(&#34;disabled&#34;, true); $(DropdownlistGLAcct).prop(&#34;disabled&#34;, true); $(DropdownlistGLAcct).closest(&#39;td&#39;).find(&#39;button&#39;).addClass(&#34;disabled&#34;); } } } }); }); }); &lt;/script&gt; &lt;div&gt; &lt;asp:Label ID=&#34;LabelFullname&#34; Text=&#34;Test&#34; runat=&#34;server&#34; /&gt; &lt;asp:GridView ID=&#34;gvtfaTransaction&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; OnRowDataBound=&#34;gvtfaTrust_RowDataBound&#34; DataKeyNames=&#34;ID&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ID&#34; HeaderText=&#34;0. ID&#34; ShowHeader=&#34;false&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;ID&#34; /&gt; &lt;asp:BoundField DataField=&#34;BankAccount&#34; HeaderText=&#34;1. Bank Account&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;BankAccount&#34; Visible=&#34;false&#34; /&gt; &lt;asp:BoundField DataField=&#34;DateDeposited&#34; HeaderText=&#34;2. Date Deposited&#34; DataFormatString=&#34;{0:dd, MMM yyyy}&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;DateDeposited&#34; /&gt; &lt;asp:BoundField DataField=&#34;PayName&#34; HeaderText=&#34;3. Payee&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;PayName&#34; /&gt; &lt;asp:BoundField DataField=&#34;Reference&#34; HeaderText=&#34;4. Reference&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;Reference&#34; /&gt; &lt;asp:BoundField DataField=&#34;Memo&#34; HeaderText=&#34;5. Memo&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;Memo&#34; /&gt; &lt;asp:BoundField DataField=&#34;EntryType&#34; HeaderText=&#34;6. Entry Type&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;EntryType&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;7. GL Account&#34; HeaderStyle-HorizontalAlign=&#34;Left&#34; SortExpression=&#34;GLAcct&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;LabelGLAcct&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GLAcct&#34;) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;asp:DropDownList ID=&#34;DropdownlistGLAcct&#34; runat=&#34;server&#34; Visible=&#34;false&#34; CssClass=&#34;selectpicker show-tick form-control Update&#34; data-style=&#34;btn btn-default btn-sm&#34; data-live-search-style=&#34;true&#34; data-live-search=&#34;true&#34;&gt; &lt;asp:ListItem Text=&#34;text 1&#34; /&gt; &lt;asp:ListItem Text=&#34;text 2&#34; /&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;HeaderStyle HorizontalAlign=&#34;Left&#34; Wrap=&#34;false&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;Amount&#34; HeaderText=&#34;8. Amount&#34; DataFormatString=&#34;{0:C}&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;Amount&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;9. Exchange&#34; SortExpression=&#34;ExchangeRate&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;LabelExchangeRate&#34; runat=&#34;server&#34; Text=&#39;&lt;%# String.Format(&#34;{0:0.00000}&#34;, Eval(&#34;ExchangeRate&#34;)) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;TextExchangeRate&#34; Text=&#39;&lt;%# Eval(&#34;ExchangeRate&#34;, &#34;{0:0.00000}&#34;) %&gt;&#39; CssClass=&#34;btn-sm form-control Update&#34; Width=&#34;65px&#34; Enabled=&#39;&lt;%# Eval(&#34;IsExchangeRate&#34;) %&gt;&#39; data-mask=&#34;#.##000&#34; /&gt; &lt;/ItemTemplate&gt; &lt;HeaderStyle Wrap=&#34;False&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;10. Details&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;details&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;btnSelect&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Details&#34;) %&gt;&#39; CommandName=&#34;Select&#34; CssClass=&#34;Select&#34; CommandArgument=&#34;&lt;%# Container.DataItemIndex %&gt;&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;11. Mark&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;Marked&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;btnMark&#34; runat=&#34;server&#34; Checked=&#39;&lt;%# Eval(&#34;Marked&#34;) %&gt;&#39; CssClass=&#34;form-check-input Mark&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;12. Ignore&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;Ignore&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;btnIgnore&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Checked=&#39;&lt;%# Eval(&#34;Ignore&#34;) %&gt;&#39; OnCheckedChanged=&#34;Ignore_Click&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;SelectedRowStyle BackColor=&#34;lightyellow&#34; /&gt; &lt;PagerSettings Mode=&#34;NumericFirstLast&#34; /&gt; &lt;PagerStyle CssClass=&#34;pagination-ys&#34; HorizontalAlign=&#34;Left&#34; /&gt; &lt;/asp:GridView&gt; &lt;asp:GridView ID=&#34;gvtfaDetails&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;4. Check Memo&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;LabelCheckMemo&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextCheckMemo&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&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.Web.Services; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Web.Services 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 (!this.IsPostBack) { this.BindGrid(); this.BindDummyRow(); } } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;ID&#34;, typeof(int)), new DataColumn(&#34;BankAccount&#34;, typeof(string)), new DataColumn(&#34;DateDeposited&#34;, typeof(string)), new DataColumn(&#34;PayName&#34;, typeof(string)), new DataColumn(&#34;Reference&#34;, typeof(string)), new DataColumn(&#34;Memo&#34;, typeof(string)), new DataColumn(&#34;EntryType&#34;, typeof(int)), new DataColumn(&#34;GLAcct&#34;, typeof(string)), new DataColumn(&#34;Amount&#34;, typeof(int)), new DataColumn(&#34;ExchangeRate&#34;, typeof(int)), new DataColumn(&#34;IsExchangeRate&#34;, typeof(bool)), new DataColumn(&#34;Details&#34;, typeof(string)), new DataColumn(&#34;Marked&#34;, typeof(bool)), new DataColumn(&#34;Ignore&#34;, typeof(bool)) }); dt.Rows.Add(1, &#34;111&#34;, &#34;01/02/2018&#34;, &#34;BN&#34;, &#34;123455AAA&#34;, &#34;CM&#34;, 124, &#34;GA&#34;, 100, 5, true, &#34;Details&#34;, true, true); dt.Rows.Add(2, &#34;222&#34;, &#34;02/02/2018&#34;, &#34;BN1&#34;, &#34;123455AAA112&#34;, &#34;CM1&#34;, 12411, &#34;GA1&#34;, 200, 6, true, &#34;Details&#34;, true, false); gvtfaTransaction.DataSource = dt; gvtfaTransaction.DataBind(); } private void BindDummyRow() { DataTable dummy = new DataTable(); dummy.Columns.Add(&#34;CustomerId&#34;); dummy.Columns.Add(&#34;Name&#34;); dummy.Columns.Add(&#34;Country&#34;); dummy.Rows.Add(); gvtfaDetails.DataSource = dummy; gvtfaDetails.DataBind(); } protected void Ignore_Click(object sender, EventArgs e) { } [WebMethod] public static string GetData(int custid) { string strconstr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strconstr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers WHERE CustomerId=@CustomerId&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, custid); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataSet ds = new DataSet()) { da.Fill(ds); return ds.GetXml(); } } } } } [WebMethod()] public static string Mark(int id, string Marked, string LabelFullname) { try { int age = int.Parse(&#34;1&#34;); return &#34;success&#34;; } catch (Exception ex) { return &#34;failed&#34;; } } protected void gvtfaTrust_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[11].Controls.OfType&lt;CheckBox&gt;().FirstOrDefault().Checked) { e.Row.Cells[7].Controls.OfType&lt;DropDownList&gt;().FirstOrDefault().Enabled = false; e.Row.Cells[7].Controls.OfType&lt;DropDownList&gt;().FirstOrDefault().Visible = true; e.Row.Cells[7].Controls.OfType&lt;Label&gt;().FirstOrDefault().Visible = true; e.Row.Cells[9].Controls.OfType&lt;TextBox&gt;().FirstOrDefault().Enabled = false; e.Row.Cells[12].Controls.OfType&lt;CheckBox&gt;().FirstOrDefault().Enabled = false; } } }</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 Me.BindGrid() Me.BindDummyRow() End If End Sub Private Sub BindGrid() Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;ID&#34;, GetType(Integer)), New DataColumn(&#34;BankAccount&#34;, GetType(String)), New DataColumn(&#34;DateDeposited&#34;, GetType(String)), New DataColumn(&#34;PayName&#34;, GetType(String)), New DataColumn(&#34;Reference&#34;, GetType(String)), New DataColumn(&#34;Memo&#34;, GetType(String)), New DataColumn(&#34;EntryType&#34;, GetType(Integer)), New DataColumn(&#34;GLAcct&#34;, GetType(String)), New DataColumn(&#34;Amount&#34;, GetType(Integer)), New DataColumn(&#34;ExchangeRate&#34;, GetType(Integer)), New DataColumn(&#34;IsExchangeRate&#34;, GetType(Boolean)), New DataColumn(&#34;Details&#34;, GetType(String)), New DataColumn(&#34;Marked&#34;, GetType(Boolean)), New DataColumn(&#34;Ignore&#34;, GetType(Boolean))}) dt.Rows.Add(1, &#34;111&#34;, &#34;01/02/2018&#34;, &#34;BN&#34;, &#34;123455AAA&#34;, &#34;CM&#34;, 124, &#34;GA&#34;, 100, 5, True, &#34;Details&#34;, True, True) dt.Rows.Add(2, &#34;222&#34;, &#34;02/02/2018&#34;, &#34;BN1&#34;, &#34;123455AAA112&#34;, &#34;CM1&#34;, 12411, &#34;GA1&#34;, 200, 6, True, &#34;Details&#34;, True, False) gvtfaTransaction.DataSource = dt gvtfaTransaction.DataBind() End Sub Private Sub BindDummyRow() Dim dummy As DataTable = New DataTable() dummy.Columns.Add(&#34;CustomerId&#34;) dummy.Columns.Add(&#34;Name&#34;) dummy.Columns.Add(&#34;Country&#34;) dummy.Rows.Add() gvtfaDetails.DataSource = dummy gvtfaDetails.DataBind() End Sub Protected Sub Ignore_Click(ByVal sender As Object, ByVal e As EventArgs) End Sub &lt;WebMethod()&gt; Public Shared Function GetData(ByVal custid As Integer) As String Dim strconstr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strconstr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers WHERE CustomerId=@CustomerId&#34;, con) cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, custid) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Using ds As DataSet = New DataSet() da.Fill(ds) Return ds.GetXml() End Using End Using End Using End Using End Function &lt;WebMethod()&gt; Public Shared Function Mark(ByVal id As Integer, ByVal Marked As String, ByVal LabelFullname As String) As String Try Dim age As Integer = Integer.Parse(&#34;1&#34;) Return &#34;success&#34; Catch ex As Exception Return &#34;failed&#34; End Try End Function Protected Sub gvtfaTrust_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.Cells(11).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked Then e.Row.Cells(7).Controls.OfType(Of DropDownList)().FirstOrDefault().Enabled = False e.Row.Cells(7).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = True e.Row.Cells(7).Controls.OfType(Of Label)().FirstOrDefault().Visible = True e.Row.Cells(9).Controls.OfType(Of TextBox)().FirstOrDefault().Enabled = False e.Row.Cells(12).Controls.OfType(Of CheckBox)().FirstOrDefault().Enabled = False End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/ffm3C2D.gif" alt="" width="468" height="114" /></p>https://www.aspforums.net:443/Threads/378130/Populate-ASPNet-GridView-on-another-GridView-Selected-Row-using-jQuery-AJAX-and-WebMethod/https://www.aspforums.net:443/Threads/378130/Populate-ASPNet-GridView-on-another-GridView-Selected-Row-using-jQuery-AJAX-and-WebMethod/Fri, 04 Jan 2019 04:12:12 GMT