ReorderList with ObjectDataSource

If you ask me, the AJAX Control Toolkit's ReorderList is the most under appreciated control in the Toolkit.  Compared to many of the other controls, it gets no love.  The Toolkit demo site's sample for this control isn't really all that helpful and third party samples are sparse.  And how do you bind it with an ObjectDataSource control?  Read on to find out...

The ReorderList had a 'real word' use for me.  There was a situation where I inherited a web form with a priority ranked list that was composed of a list of items that each had a textbox control to the left of them.  The textbox contained the priority order for the item in the list.  If the user wanted to change the order, he or she would change the numbers in the textboxes and then press a Save button which updated the priority order for the list.  Obviously, the users were having a difficult time making this work.

In comes the ReorderList, the magical key to this situation.  But how does this work; the Toolkit sample site didn't go far enough.  I wanted to bind this with an ObjectDataSource control and it just wasn't working.  Well I eventually figured it out and the first thing I thought of was I needed to write an article about this.  Of course this was over 6 months ago but now that I have this blog I thought that someone may benefit from my pain.

I am including the sample project for this but let's walk through it.
First thing's first - we need some data to reorder.  How about a Wish List?  The below image shows a WishList table which is included in the project in a SQLExpress database.  There are only three columns: WishID (uniqueidentifier) which is the primary key; WishDescription (varchar) which is the wish text; and WishOrder (int) which holds the order of my wishes.  It is pictured here:

DatabseTable

So next create a class in App_Code for the ObjectDataSource to bind to.  I've called it ReorderClass.  First we need to have a method return a DataSet containing the wish data in from the database.  Here is the code for that:
DatasetCode 



Since the order number from the ReorderList needs to be saved back in the database, we also need an Update method for our ObjectDataSource control.  The key here that many people miss is the Update method MUST have a method parameter for each of the data items that were selected in the above DataSet.  Not only is that true, but the method signatures must also be the same data type of the items returned from the Select method's DataSet.  Yes, even if you are not updating all the fields, you still need all the Selected fields in the Update method's signature.  So the DataSet in the Select method returned WishID, WishDescription, and WishOrder.  So, this Update method needs all three of those fields in its method signature.  The three method parameters are WishID which is of type Guid, WishDescription of type string, and WishOrder of type integer.  The code for this is here: 

UpdateCode

Now it's time for the ReorderList control.  You can bind data to the ReorderList but unlike the GridView control, there is no built-in auto binding option.  You have to create the template to bind the data to.  Here I've created a table for that contains the Wish Order number and Wish Description.  Also note that the ReorderList has properties including SortOrderField and DataKeyField.  The SortOrderField needs to be bound to the data item containing the sort order for your items.  The DataKeyField needs to be bound to your data item's identifier field (most likely its primary key in the database).

ControlCode

Now we're going to bind this to an ObjectDataSource control.  Notice that the Select Method is the method that returns the DataSet.  Also notice that the UpdateParameters match the fields that were in the Select statement.  Since there is no option for Type of uniqueidentifier, the wishID Parameter has been set to type of Object.

DataSourceCode

Now that it's all together, it will load, reorder, and save the reorder.

image 

It's not the simplest tool in the Toolkit to use but it is one of the coolest for sure.

Published Friday, February 22, 2008 8:36 PM by justinsaraceno

Comments

# re: ReorderList with ObjectDataSource

Thursday, March 06, 2008 12:10 AM by Dave

Thanks, I was trying to figure this out found another example that had the only way to accomplish this storing items in session state, and this one helped me do it without sessions. Awesome write up, and the key about making sure the number of values match was my last obstacle. I'll be resuing this throughout my application very helpful very helpful.

# re: ReorderList with ObjectDataSource

Friday, May 16, 2008 12:07 AM by Muhammad Shahid Usman

its really good feature that you have explore in this example. Good job

Thanks

# re: ReorderList with ObjectDataSource

Friday, May 30, 2008 4:28 AM by angshujit

Thanks a lot. For last two days I have search something exactly like this. Just now I have tried it out with my own application and it works fine.

Thank you again.

# re: ReorderList with ObjectDataSource

Friday, July 04, 2008 11:35 AM by roberto.alarcon

Thanks a lot, I have been trying to make the Reorderlist work and all the samples are really poors, but you have the real solutions and EXPLANAITIONS of how this component work.

Very good article.

# re: ReorderList with ObjectDataSource

Wednesday, July 16, 2008 2:38 PM by darklord

well done...and very very helpful

# re: ReorderList with ObjectDataSource

Wednesday, July 23, 2008 7:23 PM by JJ

Thanks so much.  Works great, and just what I needed!

# re: ReorderList with ObjectDataSource

Friday, August 01, 2008 2:01 PM by kmckinney

I am trying to bind this to a list of objects using a objectdatasource.  I am having some issues with the functionality when i do an edit or add, can you point me to a good place to resolve these issues.

# Reorderlist - save to database with SqlDataSource

Thursday, August 14, 2008 6:19 PM by ASP.NET AJAX Toolkit Forum Posts

Hi Guy's I'm trying to figure out a generic way to save(update) a reordered list to the database

# re: ReorderList with ObjectDataSource

Thursday, May 07, 2009 7:15 PM by Negron

Great article.

You helped me a lot.

Regards.

# re: ReorderList with ObjectDataSource

Wednesday, May 13, 2009 2:23 AM by sgeray

Thank you, you saved my day. I had another page that working but could not understand what i am missing on new one. The point , update parameters does not match with select fields.

Regards.

# re: ReorderList with ObjectDataSource

Monday, June 22, 2009 1:49 PM by iziizi

This works great.

However, when I delete an Item, it doesnt automatically re-assign orders. Not that this really matters but it means I end up with the orders as 2,3,5,6,7,10 etc...

Any ideas?

# re: ReorderList with ObjectDataSource

Friday, June 26, 2009 9:23 AM by Gamuchirai Christopher Chaita

You're a life saver

# re: ReorderList with ObjectDataSource

Friday, August 21, 2009 8:57 AM by knuebbelAAG

don't work with version 30512 ???

# Triplett Web

Monday, August 24, 2009 4:21 PM by Triplett Web

Pingback from  Triplett Web

# re: ReorderList with ObjectDataSource

Tuesday, September 29, 2009 10:25 AM by HugoGonalves

You save my life! :)

Thanks

# re: ReorderList with ObjectDataSource

Saturday, December 05, 2009 4:38 PM by Bob

I can't make any reordering (movements) ?

Haven't changed the code... just ran it from inside Visual Studio 2005 ?!

# re: ReorderList with ObjectDataSource

Friday, January 15, 2010 8:13 AM by Mital

Hi, This was a great help but When i drag and drop the item my wishorder value changes in database... any chance u know the solution, please help me.

Thanks

# re: ReorderList with ObjectDataSource

Monday, January 18, 2010 6:40 AM by FaMu

I follow the tutorial and it's working great but when I modified it the order can't be saved to database

forums.asp.net/.../1515054.aspx

stackoverflow.com/.../why-this-reorderlist-cant-be-saved-to-database

please help

# re: ReorderList with ObjectDataSource

Monday, June 07, 2010 5:13 AM by Senthil

I have three columns in reorderlist ,when I bind a list in that reorderlist,only two columns are binding and remaining is not binding

# Re-Order List items not moveable | The Largest Forum Archive

Sunday, September 26, 2010 4:32 AM by Re-Order List items not moveable | The Largest Forum Archive

Pingback from  Re-Order List items not moveable | The Largest Forum Archive

# re: ReorderList with ObjectDataSource

Thursday, November 11, 2010 12:13 PM by Kamui

Brilliant Article helped me get my head around this. Thank you.

# re: ReorderList with ObjectDataSource

Friday, February 04, 2011 12:36 PM by Kapil

This code is not working for me. Is this code compatible with IE? You code works fine in Google Chrome but not in IE.

I Tried the same logic for my project and I observed its not calling function ReorderWishList() at all. In my DB Table I have WishID as "INT, Primary Key". I don't know where I am going wrong. Help will be greatly appreciated.

Thanks.

# ???????????????????????????????????????????????? | IT??????????????????IT??????????????????

Pingback from  ???????????????????????????????????????????????? | IT??????????????????IT??????????????????

# re: ReorderList with ObjectDataSource

Friday, March 25, 2011 9:13 AM by mohan sharma

Good One. This helped me a lot. I was googling it for a long time.

Thank you very much

# re: ReorderList with ObjectDataSource

Tuesday, March 29, 2011 4:47 PM by Norrin Radd

Good Article. This helped me a lot. Really appreciated.

Thanks so much & aloha!

# Implementing user defined display order UI - Programmers Goodies

Pingback from  Implementing user defined display order UI - Programmers Goodies

Leave a Comment

(required) 
(required) 
(optional)
(required)