Nirmal KumarC

Nirmal KumarC

  • 1.6k
  • 230
  • 49.9k

How to data bind gridview(bootsrap Datatable) using jquery

Apr 3 2019 11:55 PM
Hi,
How to data bind gridview bootsrap Datatable using jquery in asp.net master page then below code wise but data bind multiple display this page then Searching,Sorting,Paging also not working so please helpme how tosolve this issue
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Employee/FinalMenu_Master.master" AutoEventWireup="true"  
  2. CodeFile="DW_Tasks.aspx.cs" Inherits="Employee_DW_Tasks" %>  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">  
  4. <link type="text/css" href="Design/css/dataTables.bootstrap.min.css" rel="stylesheet" />  
  5. <link type="text/css" href="Design/css/responsive.bootstrap.min.css" rel="stylesheet" />  
  6. <script type="text/jscript" src="Design/js/jquery.dataTables.min.js"></script>  
  7. <script type="text/javascript" src="Design/js/dataTables.bootstrap.min.js"></script>  
  8. <script type="text/javascript" src="Design/js/dataTables.responsive.min.js"></script>  
  9. <script type="text/javascript" src="Scripts/jquery.min.js"></script>  
  10. <link href="Design/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet" />  
  11. <script type="text/javascript">  
  12. $(function () {  
  13. $('[id*=gvCustomers]').prepend($("<thead></thead>").append($(this).find("tr:first"))).DataTable({  
  14. "responsive"true,  
  15. "sPaginationType""full_numbers"  
  16. });  
  17. });  
  18. </script>  
  19. <script type="text/javascript">  
  20. $(function () {  
  21. GetCustomers();  
  22. });  
  23. function GetCustomers() {  
  24. // debugger;  
  25. $.ajax({  
  26. type: "POST",  
  27. url: "DW_Tasks.aspx/GetCustomers",  
  28. data: '{}',  
  29. contentType: "application/json; charset=utf-8",  
  30. dataType: "json",  
  31. success: OnSuccess,  
  32. failure: function (response) {  
  33. alert(response.d);  
  34. },  
  35. error: function (response) {  
  36. alert(response.d);  
  37. }  
  38. });  
  39. }  
  40. function OnSuccess(response) {  
  41. // debugger;  
  42. var xmlDoc = $.parseXML(response.d);  
  43. var xml = $(xmlDoc);  
  44. var customers = xml.find("Customers");  
  45. var row = $("[id*=gvCustomers] tr:last-child").clone(true);  
  46. $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();  
  47. $.each(customers, function () {  
  48. // debugger;  
  49. var customer = $(this);  
  50. $("td", row).eq(0).html($(this).find("CustomerID").text());  
  51. $("td", row).eq(1).html($(this).find("ContactName").text());  
  52. $("td", row).eq(2).html($(this).find("City").text());  
  53. $("[id*=gvCustomers]").append(row);  
  54. row = $("[id*=gvCustomers] tr:last-child").clone(true);  
  55. });  
  56. };  
  57. </script>  
  58. </asp:Content>  
  59. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">  
  60. <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >  
  61. <Columns>  
  62. <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />  
  63. <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />  
  64. <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />  
  65. </Columns>  
  66. </asp:GridView>  
  67. </asp:Content>  
  1. private void BindDummyRow()  
  2. {  
  3. DataTable dummy = new DataTable();  
  4. dummy.Columns.Add("CustomerID");  
  5. dummy.Columns.Add("ContactName");  
  6. dummy.Columns.Add("City");  
  7. dummy.Rows.Add();  
  8. gvCustomers.DataSource = dummy;  
  9. gvCustomers.DataBind();  
  10. }  
  11. [WebMethod]  
  12. public static string GetCustomers()  
  13. {  
  14. DataTable dt = new DataTable();  
  15. dt.TableName = "Customers";  
  16. dt.Columns.Add("CustomerID"typeof(string));  
  17. dt.Columns.Add("ContactName"typeof(string));  
  18. dt.Columns.Add("City"typeof(string));  
  19. DataRow dr1 = dt.NewRow();  
  20. dr1["CustomerID"] = 1;  
  21. dr1["ContactName"] = "Customer1";  
  22. dr1["City"] = "City1";  
  23. dt.Rows.Add(dr1);  
  24. DataRow dr2 = dt.NewRow();  
  25. dr2["CustomerID"] = 2;  
  26. dr2["ContactName"] = "Customer2";  
  27. dr2["City"] = "City2";  
  28. dt.Rows.Add(dr2);  
  29. DataRow dr3 = dt.NewRow();  
  30. dr3["CustomerID"] = 3;  
  31. dr3["ContactName"] = "Customer3";  
  32. dr3["City"] = "City3";  
  33. dt.Rows.Add(dr3);  
  34. DataRow dr4 = dt.NewRow();  
  35. dr4["CustomerID"] = 4;  
  36. dr4["ContactName"] = "Customer4";  
  37. dr4["City"] = "City4";  
  38. dt.Rows.Add(dr4);  
  39. DataRow dr5 = dt.NewRow();  
  40. dr5["CustomerID"] = 5;  
  41. dr5["ContactName"] = "Customer5";  
  42. dr5["City"] = "City5";  
  43. dt.Rows.Add(dr5);  
  44. DataSet ds = new DataSet();  
  45. ds.Tables.Add(dt);  
  46. return ds.GetXml();  
  47. }  

Answers (1)