Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to implement JQuery UI Sortable in DataList Control ?

0.00/5 (No votes)
26 Aug 2011 1  
Basically when DataList is rendered HTML in Table and like below   HTML Code placed at ItemTemplate

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.


Basically when DataList is rendered HTML in Table and like below 
<table>
<tr>
  <td> HTML Code placed at ItemTemplate </td>
</tr>

</table>

But UI sortable can be apply on <tbody>, so we need to add tbody on the above table to implement Jquery UI Sortable.

But now how we can inject tbody in to a DataList ? it’s too simple to do that.

<asp:DataList ID="dlList" runat="server" >
        <HeaderTemplate>
<tbody>
</HeaderTemplate>
        <ItemTemplate>
</ItemTemplate>       
        <FooterTemplate>
</tbody>
</FooterTemplate>
</asp:DataList>
     
By adding  <tbody> start tag at HeaderTemplate and </tbody> end tag at the FooterTemplate we can inject the <tbody> in the DataList .
 After adding the tbody in Header & Footer Template the rendered HTML become will be
<table>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>

Now we can easily implement JQuery UI Sortable in the DataList below the Jquery implementation.
For that you need to add below Js in your application 
 
$(function () {
             $("#<%=dlProcessList.ClientID %> tbody").sortable({
                 handle: ".handle",
                 helper: tableRowHelper,
                 placeholder: 'ui-state-highlight',
                 cursor: 'move',
                 start: function (event, ui) {
                     ui.placeholder.height(ui.helper.height());
                 }
             }).disableSelection();
         });        

<asp:DataList ID="dlList" runat="server" >
        <HeaderTemplate>
<tbody>
</HeaderTemplate>
        <ItemTemplate>
// insert code to display required data
</ItemTemplate>       
        <FooterTemplate>
</tbody>
</FooterTemplate>
</asp:DataList>

Enjoy Coding :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here