Repeater Item With Drag And Drop Capabilities

There is no magic to this at all. This is the functionality which we will achieve with the help of jQuery-UI. With jQquery-UI, we can achieve lots of other features. You can visit the jQuery-UI website for more information.

jQuery-UI is a very lightweight tool to design good looking web applications. At least, if we are comfortable with or at least understand HTML, CSSS and JavaScript, jQuery easily, because jQuery-UI uses the same.

We use repeater to display the content. Sometimes we want the features of drag and drop for comparison purposes. Basically, Repeater is a very lightweight server side control to display the data-source or the list items.

For Repeater control, you may visit my article:

My suggestion is if you have not worked with Repeater, first visit the link given above and learn about it.

Notes given on jQuery-ui.com for Drag and Drop

Enable draggable functionality on any DOM element. Move the draggable object by clicking on it with the mouse and dragging it anywhere within the viewport.

Now, we will start implementing the drag and drop feature to Repeater items.

Step by Step

Create an ASP.NET Website project named “RepeaterItemWithDragNDrop”.



After successful creation of the project in your Solution Explorer, it will look as shown below.



Right click on project title I.e. “RepeaterItemWithDragNDrop” .



Select WebForm, as shown in the image given below.



Now, click on ToolBox and select Repeater control, which is under the DATA option.



Drag and drop Repeater control on Default.aspx page.



Your default.aspx will be displayed in design view, as shown above.

Right click on the project title I.e. “RepeaterItemWithDragNDrop” in bold letters.



Select LINQ TO SQL Class and name it as “FriendListDataClass.dbml”.



As you click ADD button in the screen, shown above, you will see the dialog box.



Simply click Yes.

Now, switch to the Server Explorer and select Data Connections.

Table Structure
  1. SET ANSI_NULLS ON  
  2. GO  
  3.   
  4. SET QUOTED_IDENTIFIER ON  
  5. GO  
  6.   
  7. SET ANSI_PADDING ON  
  8. GO  
  9.   
  10. CREATE TABLE [dbo].[tblTechFriends](  
  11. [FriendID] [int] IDENTITY(1,1) NOT NULL,  
  12. [Name] [varchar](50) NULL,  
  13. [EmailAddress] [varchar](150) NULL,  
  14. [TotalArticles] [intNULL  
  15. ON [PRIMARY]  
  16.   
  17. GO  
  18.   
  19. SET ANSI_PADDING OFF  
  20. GO  
Sample Datas



Right click on Data Connections and select ADD CONNECTION option.



After successful connection establishment with the Database Server, your Server Explorer will look, as shown below.



Double click your DBML file, which is located inside the APP_CODE folder.

Drag and drop the table named “ tblTechFriends” on DBML canvas.



Now, double click on “Default.aspx” page.

Insert the Repeater code in default.aspx page.
  1. <asp:Repeater ID="rptTechFriends" runat="server">  
  2.     <ItemTemplate>  
  3.         <div id="draggable" class="draggable ui-widget-content">  
  4.             <asp:Label ID="lblFriendName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>  
  5.             <br /> Email Address : <b><asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress")%>'></asp:Label></b>  
  6.             <br /> Total Articles : <b><asp:Label ID="lblTotalArticles" runat="server" Text='<%# Eval("TotalArticles")%>'></asp:Label></b>  
  7.         </div>  
  8.     </ItemTemplate>  
  9. </asp:Repeater>  
In the repeater given above, we are showing FRIEND’S Name, EmailAddress and Total Articles.

What are the important lines of code?
  1. For stylesheet of jQuery-UI.css, we used CDN
    1. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
  2. For jQuery reference, we used CDN.
    1. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>  
  3. For jQuery-ui.js reference, we used CDN.
    1. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>  
  4. We created a style called: 
    1. .draggable { width: 225px; height: 100px; padding: 0.5em; }  
    This is assigned to all divs of Repeater. 

  5. jQuery function
    1. <script>  
    2.     $(function() {  
    3.         $(".draggable").draggable();  
    4.     });  
    5. </script>  

In the code given above, we are assigning the drop and drag functionalities to all DIVs, which have a class called “draggable”.

Full code of Default.aspx page

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html>  
  3.   
  4.     <html xmlns="http://www.w3.org/1999/xhtml">  
  5.   
  6.     <head id="Head1" runat="server">  
  7.         <meta name="viewport" content="width=device-width, initial-scale=1">  
  8.         <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
  9.         <style>  
  10.             .draggable {  
  11.                 width: 225px;  
  12.                 height: 100px;  
  13.                 padding: 0.5em;  
  14.             }  
  15.         </style>  
  16.         <script src="https://code.jquery.com/jquery-1.12.4.js"></script>  
  17.         <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>  
  18.         <script>  
  19.             $(function() {  
  20.                 $(".draggable").draggable();  
  21.             });  
  22.         </script>  
  23.         <title>Drag N Drop</title>  
  24.     </head>  
  25.   
  26.     <body>  
  27.         <form id="form1" runat="server">  
  28.             <div>  
  29.                 <asp:Repeater ID="rptTechFriends" runat="server">  
  30.                     <ItemTemplate>  
  31.                         <div id="friendDetail" class="draggable ui-widget-content">  
  32.                             <asp:Label ID="lblFriendName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>  
  33.                             <br /> Email Address : <b><asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress")%>'></asp:Label></b>  
  34.                             <br /> Total Articles : <b><asp:Label ID="lblTotalArticles" runat="server" Text='<%# Eval("TotalArticles")%>'></asp:Label></b>  
  35.                         </div>  
  36.                     </ItemTemplate>  
  37.                 </asp:Repeater>  
  38.             </div>  
  39.         </form>  
  40.     </body>  
  41.   
  42.     </html>  
Full code of Default.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class _Default: System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)   
  11.     {  
  12.         if (!IsPostBack) {  
  13.             var db = new FriendListDataClassDataContext();  
  14.             rptTechFriends.DataSource = db.tblTechFriends.ToList();  
  15.             rptTechFriends.DataBind();  
  16.         }  
  17.     }  
  18. }  
Output

By default, the page will render as shown below.


By default, the page will load as shown above.

Later on, move the box (div) anywhere with the Browser screen.

I arranged the box per total articles.

Example 1


Example 2



You can move the DIV anywhere.

Up Next
    Ebook Download
    View all
    Learn
    View all