Click here to Skip to main content
6,595,444 members and growing! (21,521 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Sorting Gridview using Jquery with ASP.NET

By Sailung Limbu

This article demonstrate flexible client-side table sorting
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), Javascript, .NET (.NET 2.0), ASP.NET, ADO.NET, Ajax, Dev
Posted:20 Oct 2008
Views:12,901
Bookmarked:28 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
1 vote for this article.
Popularity: 0.00 Rating: 4.00 out of 5

1

2

3
1 vote, 100.0%
4

5

Introduction

This is my first article; I want to apologize in my English. In this example, I want to show how to sorting a gridview using a Jquery in ASP.NET, it is a flexible client-side table sorting. We have known that Jquery is a new kind of Javascript Library. You can find detail of Jquery in this site http://jquery.com/ there are lot of example with documentation and also allow free download a latest version.

Background

In this example, I used table sorter plug-in which has written by Christian Bach can found http://tablesorter.com/docs/ . It is really cool plug-in and user can allow to download and customization as their wish.

I test this example in Visual Studio 2005. Some of other requirement needed is download a latest version of Jquery from official website and table sorter plug-in which can found as I mention above website.

Here we go,

Using the Code

First, we need some image file which require for click in Gridview header. Then, we need to add a CSS file to make proper position and color.

In this example, I use ajax json serialize to load data into the Gridview

<%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="TableSorterBlue.aspx.vb" Inherits="TableSorterBlue" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Table Sorter Blue</title>
<link href="App_Themes/bluestyle.css" rel="stylesheet" type="text/css" media="print, projection, screen"/>
<script language="javascript" type="text/javascript" src="script/jquery-1.2.6.min.js"></script>
<script language="javascript" type="text/javascript" src="script/jquery.tablesorter.js"></script>
<script language="javascript" type="text/javascript" src="script/Sorter.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="btnInfo">
<button id="btnBlueLoad" type="button">Load Data</button>
</div>
<div id="result">
</div>
</form>
<script type="text/javascript">
$(function(){
    $("#btnBlueLoad").click(function(){
        LoadRecord();
    });
});
function LoadRecord(){
    $.ajax({
type: "POST",
url: "TableSorterBlue.aspx/GetOrderDetailTable",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function(msg){
        $("#result").html(msg);
        formatSorterTable();
         }
    });
}
</script>
</body>
</html>

In the code behind a Page of TableSorterBlue.aspx :

Partial Class TableSorterBlue
Inherits System.Web.UI.Page
<WebMethod(enablesession:=True)> _
Public Shared Function GetOrderDetailTable() As String
    Dim page As New Page()
    Dim userControl As UserControl = DirectCast(page.LoadControl(
        "~/controls/GridViewControl.ascx"), UserControl)
    userControl.EnableViewState = False
    Dim form As New HtmlForm()
    form.Controls.Add(userControl)
    page.Controls.Add(form)
    Dim textWriter As New StringWriter()
    HttpContext.Current.Server.Execute(page, textWriter, False)
    Return Clean(textWriter.ToString())
End Function
Private Shared Function Clean(ByVal html As String) As String
    Return Regex.Replace(html, "<[/]?(form)[^>]*?>", "", RegexOptions.IgnoreCase)
End Function
End Class

And in a user control I create a Gridview but make sure you make accessible property to true as what I did in the code:

Partial Class controls_GridViewControl
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Me.Load
    Me.MakeAccessible(Me.gvTSOrderDetail)
End Sub
Private Sub MakeAccessible(ByVal grd As GridView)
    If grd IsNot Nothing AndAlso grd.Rows.Count > 0 Then
        grd.UseAccessibleHeader = True
        grd.HeaderRow.TableSection = TableRowSection.TableHeader
    End If
End Sub
End Class

TableRowSection.TableHeader can help to create a <thead> and <tbody> which is synchronize with table sorter plug-in.

Now, you can click on arrow up and down it start sorting a row.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sailung Limbu


Member

Occupation: Software Developer
Location: Nepal Nepal

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralNot working with linkbutton Pinmembercomputerhussain3:43 31 Oct '09  
GeneralIt doesn't work with Allow Paging = true PinmemberThiago Valente5:13 27 Feb '09  
GeneralExcellent Pinmemberweb5130211:58 13 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Oct 2008
Editor: Sean Ewington
Copyright 2008 by Sailung Limbu
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project