In this article I will explain how
to reorder list using jQuery and ASP.NET and the saving the item's
updated order in the database. The advantage of reordering list with
jQuery is that it is light weight, saving of bandwidth and good from
user prespective. I have used jQuery UI Sortable List to sort the items.
The changes are persisted to database via ajax request. This way of
reordering is very interactive as the items are reordered via
Drag and Drop and changes are preserved without PostBack which gives a very smooth cross browser experience.
Below
is the screenshot how the sequence of the items is sent in the form of
comma separated string to the server via ajax request.
DB part:Let's create a table and name it
Items with
the three columns ItemID, ItemName, ItemOrder. ItemOrder will contain
the sequence number of the item i.e. item's order number. I have
inserted few sample records to have some data in our table. Below is the
script for it:
Table:
Now to show the items in front end we have to make a select SP. This SP will give the records present inside the
Items table. The records will be represented in ascending order based on
ItemOrder column. Below is the script for it:
Select SP:
CREATE PROCEDURE [SelectItems] |
We
have to make a Update SP too which will come in action when the user
will reorder the list from front end. This SP will accept one argument
ItemOrder. This argument contains items order in a comma separated form (1,4,5,6,7,8..)
. Below is the script:
Update SP
CREATE PROCEDURE [UpdateItemsOrder] |
[ItemOrder] INT IDENTITY , |
FROM dbo.SPLIT(@ItemOrder, ',' ) |
SET [Items].[ItemOrder] = [ Temp ].[ItemOrder] |
INNER JOIN @ Temp [ Temp ] ON [Items].[ItemID] = [ Temp ].[ItemID] |
Above I have used a split function (
dbo.SPLIT) to split the comma separated string passed from outside into a table. Below is the script for it:
Split function:
@delimiter VARCHAR (20) = ' ' |
SET @ index = CHARINDEX(@delimiter, @text) |
VALUES ( CAST (@text AS VARCHAR (100)) ) |
VALUES ( CAST ( LEFT (@text, @ index - 1) AS VARCHAR (100)) ) |
SET @text = RIGHT (@text, ( LEN(@text) - @ index )) |
SET @text = CAST ( RIGHT (@text, ( LEN(@text) - @ index )) AS VARCHAR (100)) |
Front End Part:Business class:Let's make a Business class for Items and name it
Item. This class contains the following property ItemID, ItemName and ItemOrder. Below is the code for it:
public int ItemID { get ; set ; } |
public string ItemName { get ; set ; } |
public int ItemOrder { get ; set ; } |
ASPX code:Let's come to ASPX part. I have used
ListView to show the items in the list. The ListView will be binded with the data fetched from database via
SelectItems SP.
User will drag the items according to his needs and accordingly changes
will be preserved in the database via ajax request made to
Sortable.asmx whose code we will discuss later in this article. Below is the code of our ASPX page.
Sample.aspx:
<%@ Page Language= "C#" %> |
<%@ Import Namespace= "System.Collections.Generic" %> |
<%@ Import Namespace= "System.Collections.ObjectModel" %> |
<%@ Import Namespace= "System.Data" %> |
<%@ Import Namespace= "System.Data.SqlClient" %> |
type= "text/css" media= "all" /> |
type= "text/javascript" ></script> |
<script type= "text/javascript" > |
$( '#sortable' ).sortable({ |
placeholder: 'ui-state-highlight' , |
$( '#sortable' ).disableSelection(); |
var progressMessage = 'Saving changes... <img src="loading.gif"/>' ; |
var successMessage = 'Saved successfully!' ; |
var errorMessage = 'There was some error in processing your request' ; |
var messageContainer = $( '#message' ).find( 'p' ); |
function OnSortableUpdate( event , ui) { |
var order = $( '#sortable' ).sortable( 'toArray' ).join( ',' ).replace(/id_/gi, '' ) |
messageContainer.html(progressMessage); |
url: 'Sortable.asmx/UpdateItemsOrder' , |
data: '{itemOrder: \'' + order + '\'}' , |
contentType: 'application/json; charset=utf-8' , |
success: OnSortableUpdateSuccess, |
error: OnSortableUpdateError |
function OnSortableUpdateSuccess(response) { |
if (response != null && response.d != null ) { |
messageContainer.html(successMessage); |
messageContainer.html(errorMessage); |
function OnSortableUpdateError(xhr, ajaxOptions, thrownError) { |
messageContainer.html(errorMessage); |
<form id= "form1" runat= "server" > |
<div class = "ui-widget" id= "message" > |
<div class = "ui-state-highlight ui-corner-all" style= "margin-top: 20px; padding: 0 .7em;" > |
<asp:ListView ID= "ItemsListView" runat= "server" ItemPlaceholderID= "myItemPlaceHolder" > |
<asp:PlaceHolder ID= "myItemPlaceHolder" runat= "server" ></asp:PlaceHolder> |
<li class = "ui-state-default" id= 'id_<%# Eval("ItemID") %>' > |
<%# Eval( "ItemName" ) %></li> |
protected void Page_Load( object sender, EventArgs e) |
ItemsListView.DataSource = FindItems(); |
ItemsListView.DataBind(); |
public static IEnumerable<Item> FindItems() |
Collection<Item> items = new Collection<Item>(); |
string connectionString = |
"Data Source=YourServerName; Initial Catalog=YourDatabase; User ID=YourUserName; Password=YourPassword" ; |
using (SqlConnection connection = new SqlConnection(connectionString)) |
using (SqlCommand command = new SqlCommand()) |
command.Connection = connection; |
command.CommandText = "SelectItems" ; |
command.CommandType = CommandType.StoredProcedure; |
using (SqlDataReader dataReader = command.ExecuteReader()) |
while (dataReader.Read()) |
item.ItemID = ( int )dataReader[ "ItemID" ]; |
item.ItemName = Convert.ToString(dataReader[ "ItemName" ]); |
WebService Code:Let's
come to webservice code which will be hit by the client side to update
the item's updated order in the database. In this webservice the
UpdateItemsOrder method
is written which will accept one argument which is the updated sequence
from the client's item's list. This method will hit the the DB and call
the
UpdateItemsOrder SP and accordingly response will be returned whether the rows are updated or not. Below is the code for it:
Sortable.asmx:
using System.Collections.Generic; |
using System.Collections.ObjectModel; |
using System.Data.SqlClient; |
using System.Web.Services; |
/// Summary description for Sortable |
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] |
[System.ComponentModel.ToolboxItem( false )] |
[System.Web.Script.Services.ScriptService] |
public class Sortable : System.Web.Services.WebService |
public bool UpdateItemsOrder( string itemOrder) |
Collection<Item> items = new Collection<Item>(); |
string connectionString = |
"Data Source=YourServerName; Initial Catalog=YourDatabase; User ID=YourUserName; Password=YourPassword" ; |
using (SqlConnection connection = new SqlConnection(connectionString)) |
using (SqlCommand command = new SqlCommand()) |
command.Connection = connection; |
command.CommandText = "UpdateItemsOrder" ; |
command.CommandType = CommandType.StoredProcedure; |
SqlParameter paramUserName = new SqlParameter( "@ItemOrder" , SqlDbType.VarChar, 255); |
paramUserName.Value = itemOrder; |
command.Parameters.Add(paramUserName); |
return (command.ExecuteNonQuery() > 0); |
Below are the screenshots of the
FireBug of the requests made to the server.
Screenshot of the Post Data sent to server:ScreenShot of the Response Data received from server:
That's
it we are done with reordering list using jQuery and ASP.NET. Above you
saw how we can drag and drop the items of the list and can change their
positions according to our needs. The changes are preserved via updated
sequence sent to the server.
Do let me know your feedback, comments.
No comments:
Post a Comment