In this article we will develop Data Grid from scratch with help of Jquery based DataTable plugin and ASP.NET generic handlers. As the backend we will be using Sql Server 2008. So, lets move straight to the topic...
Note: If you find any grammatical mistake please let me know i will fix it, article is written gradually with longer span of time. And furthermore i am NOT native english speaker .
In order to understand this article you just need to have some understanding of JQuery and Underscore utility belt. We will be dealing with JSON data back and forth from server. So, you just need to be familiar with json and generic handlers too. So, our working application may structured as;
Using the code
We will follow these steps to acheive our goal:
- Client side UI
- Create Data Table
- Create Store Procedure for Data Fecthing
- Developing Generic Handler in ASP.NET as middle layer
- Few Things about NewtonSoft.Json
- Add Handler in web.config
Lets go through all these steps:
1. Client side UI
Our final client side UI will look like this
Code below show the head portion of the page. It is referencing several scripts like DataTable script link, JQuery library, noty plugin to display notifications etc. Code is heavily commented and i hope it will be very easy to grab.
Body contains place holder for datatable where DataTable will be rendered.
Page Scripting (Overview)
Our main binding script for Datatable will mainly look like this
Little Bit about Data Table Options
These are very few and commonly used data table options in our case...
For more details about data table available options, you can visit data table official plugin site.
Custom columns in above mention script will look like this
2. Create Data Table
We can create a simple table by using the SQL script, and our table will look like this;
CREATE TABLE [dbo].[hr_Customer](
[CustomerID] [bigint] IDENTITY(1,1) NOT NULL,
[CustomerCNIC] [varchar](15) NULL,
[CustomerName] [varchar](200) NOT NULL,
[CustomerAddress] [varchar](200) NOT NULL,
[OfficeTelephone] [varchar](50) NULL,
[HomeTelephone] [varchar](50) NULL,
[MobileNumber1] [varchar](50) NULL,
[MobileNumber2] [varchar](50) NULL,
[Notes] [varchar](200) NULL,
[IsActive] [bit] NOT NULL,
[EntryDate] [datetime] NOT NULL,
[ClosingDate] [datetime] NULL,
[sysDate] [datetime] NULL
CONSTRAINT [PK_hr_Customer] PRIMARY KEY CLUSTERED
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
It is just a simple table for demostration purposes, i don't want to distract readers from essential points. This structure can easily apply to larger data tables and joined views etc.
3. Create Store Procedure for Data Fecthing
Our DataTable's all data requests will be served by the stored procedure. It seems little complex but don't worry i will try to make it simple while dealing with our handler and front end UI.
ALTER proc [dbo].[sp_hr_v1_0_get_Customers]
@currentIndex int,@pageSize int,
@orderByClause varchar(max) = null, @search varchar(max) = null, @totalRecords as int output
set nocount on
declare @startIndex int
declare @endIndex int
set @startIndex = @currentIndex + 1
set @endIndex = @startIndex + @pageSize - 1
select @totalRecords = COUNT(*) from hr_Customer c where c.IsActive = 1
create table #ttt(
CustomerCNIC varchar(15) null,
CustomerName varchar(200) null,
OfficeTelephone varchar(50) null,
Notes varchar(200) null
if(@orderByClause is null and @search is null)
insert into #ttt(RowNumber,
select ROW_NUMBER() over(order by CustomerID desc),
from hr_Customer c with(readcommitted)
where c.IsActive = 1
declare @searchClause varchar(max) = '1 = 1'
if(@search is not null)
set @searchClause = '(lower(CustomerName) like lower(''' + @search + ''') + ''%''
or lower(CustomerAddress) like lower(''' + @search + ''') + ''%'')'
declare @query nvarchar(2000) = 'insert into #ttt(RowNumber,
select ROW_NUMBER() over(' + @orderByClause +'),
from hr_Customer c with(readcommitted)
where c.IsActive = 1
and ' + @searchClause
exec sp_executesql @query
select * from #ttt
where RowNumber >= @startIndex and RowNumber <= @endIndex
drop table #ttt
4. Developing Generic Handler in ASP.NET as middle layer
I have been using Visual Studio.Net 2014 for development purposes, you can use choose any .NET IDE 2008 or 2010 beside 2012. For some novice users i am adding some snapshots that to do that:
Creating Empty Solution
you can create empty web solution that will be look like this.
Inside Generic Handler
In Visual Studio.Net i am just using default setting with Framework version 4.5. It is interesting to note that we will be using a very special .NET based JSON libarary know as Newtonsoft Json library in our generic handler that i will explain later. Our Json handler look like this
In depth analysis
Lets apply divide and conqure approach to understand what is happening inside this handler; e.g. our datatable want to render its initital data it will query that from json handler, overall request will look like this
When this request is received on server then our custom handler will execute the corresponding logic to deal with it...
GetJsonCustomer will get resultant data from database and generate json result in data table expected format...
If you want to order your data depending upon the column(s) selected in datatable then this code will do that for you. For more details look the above mentioned Request Format snap.
Finally we will fetch data from database and send back datatable expected data in proper format.
Following code snippet will convert .Net DataTable into proper plugin data table expected response format. you can see that we wrap all data in aaData, i.e. expected container for data table.
Generated Response from above query will look like this.
5. Few Things about Newtonsoft.Json
As i have mentioned above that we will be using Newtonsoft.Json library to serialize our objects into proper json string and de-serialize json request received from client side into proper .NET objects. First you can have a look on this library here. It is quite easy to use and understand. I will upload this library along side my project solution for quick reference. Once we have downloaded this library, its folder structure look like this;
Once we have downloaded proper assembly we just have to add its reference in project solution, it will be seems like this;
6. Add Generic Handler in web.config
It is quite simple to add Generic Handler in solution, just right click on Project in solution explorer and choose Add > New Item > Visual C# > Web > Generic Handler. After adding it will look like this.
Add handler section in web.config file like this
Point of Interests
If you want to build data grid using extreme low level control then bare bone generic handler will be handy solution along with jquery based datatable plugin.