Asked by:
Searching,sorting on gird-view

Question
-
User639567535 posted
I try this example for search and sort but in this example they use table and i am using gridview
https://datatables.net/examples/basic_init/zero_configuration.html
i try this
public DataTable info(string id) { try { Entities2 wr = new Entities2(); List<spvechile_Result> sv = wr.spvechile(id).ToList(); DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(int)); foreach (var c in sv) { dt.Rows.Add(c.Name); } GridView1.DataSource = dt; GridView1.DataBind(); return dt; } catch (Exception ex) { throw new Exception(); } }
and html
<div class="panel" id="paneldiv"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div id="firstgrid"> <asp:GridView ID="GridView1" runat="server" CellPadding="1" Font-Names="Verdana" BackColor ="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="9pt"> <FooterStyle BackColor="White" ForeColor="#000066" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#007DBB" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#00547E" /> </asp:GridView> </div> </ContentTemplate> </asp:UpdatePanel> </div>
so how is this this possible to use this example with gridview
where as i try this with static method and jquery function but this show error
public static string datagrid(string id) { try { string result = ""; Entities2 wr = new Entities2(); List<spvechile_Result> sv = wr.spvechile(id).ToList(); DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(int)); foreach (var c in sv) { dt.Rows.Add(c.Name); } result = DataSetToJSON(dt); return result; } catch (Exception) { throw new Exception(); } //GridView1.DataSource = dt; //GridView1.DataBind(); } public static string DataSetToJSON(DataTable dt) { Dictionary<string, object> dict = new Dictionary<string, object>(); object[] arr = new object[dt.Rows.Count + 1]; for (int i = 0; i <= dt.Rows.Count - 1; i++) { arr[i] = dt.Rows[i].ItemArray; } dict.Add("response", arr); JavaScriptSerializer json = new JavaScriptSerializer(); return json.Serialize(dict); }
and client side
<script type="text/javascript"> $(function () { var id = '<%= Session["ID"] %>'; var obj = {}; obj.id = id; grid(obj); return false; }); function grid(obj) { $.ajax({ type: "POST", url: "home.aspx/datagrid", data:"{'id':'"+ obj.id +"'}", contentType: "application/json;charset=utf-8", datatype: "json", async: true, cache: false, success: function (result) { $("#tabledata").empty(); if (final.length > 0) { $("#append").append( "<thead><tr><th>Name</th></tr></thead>"); for (var i = 0; i < final.length; i++) { if (final[i] !== null) { $("#tabledata").append( "<tbody><tr><td>" + final[i][0] + "</td></tr></tbody>"); } } } else { $("#tabledata").hide(); <%-- $("#<%=Label5.ClientID%>").text("No data");--%> } }, error: function (error) { alert("errror"); } }); }; </script>
HTML
<div class="panel" id="paneldiv"> <table id="tabledata" class="display" cellspacing="0" width="100%"></table> </div> <%-- //for gird--%> <link href="Styles/grid/gfile_style.css" rel="stylesheet" /> <script src="Scripts/grid/gfile.js"></script> <script src="Scripts/grid/gfile2.js"></script>
but this show error on console
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Monday, September 5, 2016 6:13 AM
All replies
-
User-271186128 posted
Hi Bakhtawar,
Failed to load resource: the server responded with a status of 500 (Internal Server Error)As for this issue, I suggest you could set a break point to check your code in the web method, and make sure it will execute successful.
According to your code, it seems that you want to use JQuery Ajax to call the web method. If that is the case, I suggest you could refer to the following articles:
<script type="text/javascript" src="script/jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#Customers').change(function() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", data: "{ CustomerID: '" + $('#Customers').val() + "'}", url: "Customer.aspx/FetchCustomer", dataType: "json", success: function(data) { $("#CustomerDetails").html(data.d); } }); }); }); </script>
Code behind:
[WebMethod] public static string FetchCustomer(string CustomerID) { string response = "<p>No customer selected</p>"; string connect = "Server=MyServer;Database=Northwind;Trusted_Connection=True"; string query = "SELECT CompanyName, Address, City, Region, PostalCode," + "Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID"; if (CustomerID != null && CustomerID.Length == 5) { StringBuilder sb = new StringBuilder(); using (SqlConnection conn = new SqlConnection(connect)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("CustomerID", CustomerID); conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.HasRows) { while (rdr.Read()) { sb.Append("<p>"); sb.Append("<strong>" + rdr["CompanyName"].ToString() + "</strong><br />"); sb.Append(rdr["Address"].ToString() + "<br />"); sb.Append(rdr["City"].ToString() + "<br />"); sb.Append(rdr["Region"].ToString() + "<br />"); sb.Append(rdr["PostalCode"].ToString() + "<br />"); sb.Append(rdr["Country"].ToString() + "<br />"); sb.Append("Phone: " + rdr["Phone"].ToString() + "<br />"); sb.Append("Fax: " + rdr["Fax"].ToString() + "</p>"); response = sb.ToString(); } } } } } return response; }
More details, see:
http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
Best regards,
DillionMonday, September 5, 2016 8:17 AM -
User639567535 posted
for jquery method i also set debugger when i check console then this method does not execute where as in static function data succesfully visible ..
and without this jquery i try this with gridview like this
<script type="text/javascript"> $(document).ready(function () { $("#GridView1").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable({ "aoColumnDefs": [{ 'bSortable': false, 'aTargets': [3] }] }); }); </script>
and links
%-- for gridview template--%> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" /> <script type="text/javascript" src="//code.jquery.com/jquery-1.12.3.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script> <%-- for gridview template--%>
https://datatables.net/examples/styling/bootstrap.html
but this show formatting like this
so how i correct this formatting
Monday, September 5, 2016 8:32 AM