I am writing this article on demand as I got a request from one of my friends. He told me that he is looking for this requirement so I created this. As per his requirement, he needs to show data from SQL Server database table in ASP.NET GridView using jQuery, JSON and AJAX call.
Below is my Data Table from which I am showing data.
Script of my Table
- CREATE TABLE [dbo].[Customers](
- [CustomerID] [int] IDENTITY(1,1)NOTNULL,
- [Name] [varchar](50)NULL,
- [Mobile] [varchar](50)NULL,
- [City] [varchar](50)NULL,
- CONSTRAINT [PK_Customer] PRIMARYKEYCLUSTERED
- (
- [CustomerID] ASC
- )WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,
- IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,
- ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
- )ON [PRIMARY]
- GO

Now right click on Project’s Solution Explorer, then click Manage Nuget
Type jQuery and Install

Below is my aspx code
- <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm2.aspx.cs"Inherits="ExpandFillNestedGridView.WebForm2"%>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Showing Data in ASP.NET Grid View Using jQuery, JSON & AJAX Call</title>
- <script src="Scripts/jquery-2.2.0.min.js">
- </script>
- <script type="text/javascript">
- $(document).ready(function ()
- {
- $("#btnShowData").click(function ()
- {
- $.ajax
- ({
- type: "POST", contentType: "application/json; charset=utf-8", url: "WebForm2.aspx/BindCustomers", data: "{}",
- dataType: "json", success: function (result) { for (vari = 0; i<result.d.length; i++)
- {
- $( "#gvData").append( "<tr><td>" + result.d[i].CustomerID + "</td><td>" + result.d[i].Name + "</td><td>" + result.d[i].Mobile + "</td><td>" + result.d[i].City + "</td></tr>");
- }
- }, error: function (result)
- {
- alert( "Error");
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <tablestyle="background-color: yellow; border: solid 5px red; width: 100%" align="center">
- <tr>
- <tdstyle="background-color: orangered; padding: 2px; text-align: center; color: white; font-weight: bold; font-size: 14pt;">Showing Data Using jQuery, JSON & AJAX Call</td>
- </tr>
- <tr>
- <td>
- <buttonid="btnShowData" runat="server">Get Data</button>
- <br/>
- <br/>
- <formid="form1" runat="server" style="background-color:deepskyblue; padding:5px;">
- <asp:GridViewID="gvData" runat="server" CellPadding="4" ShowHeaderWhenEmpty="true" ForeColor="White" Width="100%">
- <HeaderStyleBackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <RowStyleBackColor="#EFF3FB" />
- </asp:GridView>
- </form>
- </td>
- </tr>
- </table>
- </body>
- </html>



mohamed souilmiPosted Jul 29, 2019, 6:48 AM
The data binding works perfectly but for some reason i can't activate the paging of the datagrid any idea why ??
Girish SoniPosted Jul 13, 2017, 3:56 AM
This is not working why??
Yogi SPosted Feb 8, 2017, 5:34 AM
What role json played here?
Rahul Kumar SaxenaPosted Apr 5, 2016, 8:55 AM
Thanks Imtiyaz Ansari
Rahul Kumar SaxenaPosted Apr 5, 2016, 8:55 AM
Thanks Humayun Kabir Mamun
Rahul Kumar SaxenaPosted Apr 5, 2016, 8:55 AM
Thanks Debasis Saha
Imtiyaz AnsariPosted Apr 5, 2016, 4:45 AM
Nice Article ..
Humayun Kabir MamunPosted Apr 4, 2016, 11:44 PM
Nice...
Debasis SahaPosted Apr 4, 2016, 1:44 PM
Good One..