Get Client Side and Server Side Data in Grid View Using jQuery and Ajax

Introduction

 
In this article, you will learn how to get client-side and server-side data in a Grid View using jQuery and Ajax.
 
Step 1
 
First of all, we will create a table in a .aspx page of our Application and will also add two buttons in it to get the data, either from the client-side or from the server-side.
It's coding will be something like this:
  1. <div>  
  2.      <input id="clientworkers" type="button" value="Get Client Workers Data" onclick="clientworkersdatabind()" />  
  3.      <asp:Button Text="Get Server Workers Data" runat="server" ID="Serverworkers" OnClientClick="javascript:return serverworkersdatabind()" />  
  4.      <br />  
  5.      <br />  
  6.      <table id="Workerstable">  
  7.          <thead>  
  8.              <tr>  
  9.                  <th>  
  10.                      Employee Name  
  11.                  </th>  
  12.                  <th>  
  13.                      Post  
  14.                  </th>  
  15.                  <th>  
  16.                      Age  
  17.                  </th>  
  18.                  <th>  
  19.                     Working in  
  20.                  </th>  
  21.                  <th>  
  22.                      DataSource  
  23.                  </th>  
  24.              </tr>  
  25.          </thead>  
  26.      </table>  
  27.  </div> 
Step 2
 
Now add a Script Tag and under that do the coding for the runtime evaluation and data binding.
 
It's coding will be like this:
  1. <script id="workersTemplate" type="text/html">  
  2.       <tr>  
  3.           <td> ${EmployeeName}</td>  
  4.           <td>${Post}</td>  
  5.           <td>{{if Age>30}}  
  6.               <span style='background-color:red'>Middle-aged</span>  
  7.               {{else}}  
  8.               <span style='background-color:yellow'>Still young</span>  
  9.               {{/if}}</td>  
  10.           <td>${Workingin}</td>  
  11.           <td> ${DataSource}</td>  
  12.       </tr>  
  13.   </script> 
As you can see in the third line. the runtime evaluation is done, here in this code you can use ${EmployeeName} as <%#Eval('EmployeeName')%>.
 
Step 3
 
Now we will add the data for the client and server side.
 
To add the data to the client side add the following code to the .aspx page:
  1. <script type="text/javascript">  
  2.     function clientworkersdatabind() {  
  3.         var workers = [  
  4.         {EmployeeName: "Anubhav", Post: "Developer", Age: 24, Workingin: "IT", DataSource: "Client" },  
  5.         {EmployeeName: "Ashwini", Post: "Trainee", Age: 24, Workingin: "IT", DataSource: "Client" },  
  6.         {EmployeeName: "Dinesh", Post: "Senior Manager", Age: 35, Workingin: "HR", DataSource: "Client" },  
  7.         {EmployeeName: "Swati", Post: "Tester", Age: 27, Workingin: "Testing", DataSource: "Client" },  
  8.          {EmployeeName: "Hari", Post: "Trainer", Age: 40, Workingin: "IT", DataSource: "Client" }  
  9.         ];  
  10.         BindTable(workers);  
  11.     } 
To add the data to the server-side go to the aspx.cs page and add this code:
  1. public static object ServerWorkers()  
  2. {  
  3.     List<object> workers = new List<object>();  
  4.     workers.Add(new { EmployeeName= "Anubhav", Post= "Developer", Age= 45, Workingin= "IT",DataSource="Server"});  
  5.     workers.Add(new { EmployeeName = "Ashwini", Post = "Trainee", Age = 38, Workingin = "IT", DataSource = "Server" });  
  6.     workers.Add(new { EmployeeName = "Dinesh", Post = "Senior Manager", Age = 20, Workingin = "HR", DataSource = "Server" });  
  7.     workers.Add(new { EmployeeName = "Swati", Post = "Tester", Age = 31, Workingin = "Testing", DataSource = "Server" });  
  8.     workers.Add(new { EmployeeName = "Hari", Post = "Trainer", Age = 25, Workingin = "IT", DataSource = "Server" });  
  9.      
  10.     return workers;  
Step 4
 
Now a function will be created to call the associated data on Successful execution of the code or if it's not executed successfully then it will lead to an error.
  1. function serverworkersdatabind() {  
  2.     $.ajax({  
  3.         type: "POST",  
  4.         url: "JQueryGridTemplate.aspx/ServerWorkers",  
  5.         data: "{}",  
  6.         contentType: "application/json; charset=utf-8",  
  7.         dataType: "json",  
  8.         success: function(msg, status, metaData) {  
  9.             if (msg.d && msg.d.length > 0) {  
  10.                 BindTable(msg.d);  
  11.             }  
  12.         },  
  13.         error: function(ex, status) {  
  14.             alert("error");  
  15.         },  
  16.         complete: function(eret, tytyt) {  
  17.         }  
  18.     });  
  19.     return false;  
You can download the sample code as a complete reference.
 
Output
 
It's output when it shows the Client Data:
 
client and server1.jpg
 
It's output when it shows the Server Data:
 
client and server2.jpg