Click here to Skip to main content
11,804,008 members (79,210 online)
Click here to Skip to main content

ASP.NET - Drag and Drop on a Webpage

, 28 Dec 2004 289.3K 10.5K 122
Rate this:
Please Sign up or sign in to vote.
Drag and drop contained objects on a webpage.


Many web applications are required to be complex yet intuitive to the user. One way to achieve this is to create objects or areas of the web page draggable and droppable. Unfortunately, ASP.NET server controls do not have the Drag events and DragEventArgs. Fortunately, there is a way to enable drag and drop functionalities within a web page. Now keep in mind, this is not like a Windows application where you can drag files from the user's PC to the application. The drag and drop is limited to the boundaries of the web page.

Using the code

  1. Create a web application within your solution.
  2. Create a web page.
  3. Add the provided JavaScript in the <HEAD> of your <HTML>.
  4. Add two textboxes and a button in the web form.
  5. Add the DrawContainers method to the code-behind of the web page.
  6. Ensure that the click event for the button calls the DrawContainers method.


  • Download the source code and run the sample application.


This is the JavaScript that enables the drag and drop to take place:

<!--<span class="code-comment">------- Drag and Drop JavaScript ------------></span>

C# code behind - DrawContainers method

This is the code behind used to create the drag and drop objects:

private void DrawContainers()
    TableRowCollection trCol = this.Table1.Rows;
    TableRow tr = null;
    // Should we continue?
    if(this.txtContX.Text == null || this.txtContY.Text == null)
    // Size of the row.
    int rowSize = Int32.Parse(this.txtContX.Text); 
    // Number of rows.
    int rowNumber = Int32.Parse(this.txtContY.Text);
    // Total number of containers.
    int numberOfContainers = rowSize * rowNumber;
    // Boolean value for empty table cells.
    bool isEmpty = false;
    // Loop through all of the containers.
    for(int i=0; i< numberOfContainers; i++)
        // new row mod.
        int newRow =  i % rowSize;
        // Should we create a new row?
        if(tr == null || newRow == 0)
            tr = new TableRow();

        // Empty cell generator.
            isEmpty = true;
            isEmpty = false;

        // Set the cell collection.
        TableCellCollection tdc = tr.Cells;

        // Create a new table cell.
        TableCell td = new TableCell();
        td.ID = "cell_" + i.ToString();
        // Set the cell backcolor.
        td.BackColor = Color.Turquoise;
        // Set the cell's class.
        td.CssClass = "SpecimenLoc";

        td.Attributes.Add("SpecimenId", "");

        // Is the cell empty?
            td.Attributes.Add("SpecimenId", i.ToString());
            td.BackColor = Color.Beige;
            td.Text = i.ToString();
        // Add javascript attributes to the cell.
        td.Attributes.Add("target", "true");
        td.Attributes.Add("onmousedown", "BeginDrag(;");
        td.Attributes.Add("onmouseup", "CancelDrag();"); 
        td.Width = Unit.Pixel(35);
        td.Height = Unit.Pixel(35);

        // Add the cell to the cell collection.

C# code behind - button click event

The click event of the button:

private void btnDrawContainers_Click(object sender, System.EventArgs e)
    // Draw the containers.


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


About the Author

Kenny Young
United States United States
Ken currently works as the Director of Software Engineering at the Pediatrics Epidemiology Center at the University of South Florida. He is involved in the architecture and design on numerous clinical trial projects. Some of the project include:

1. Rare Diseases Clinical Research Network
2. TrialNet - Diabetes
3. Teddy - Diabetes
4. CCOP - Cancer
5. AIDA - Diabetes

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey9-Feb-12 21:13
membermanoj kumar choubey9-Feb-12 21:13 
QuestionExplaination Pin
sakshictr10-Jan-12 19:34
membersakshictr10-Jan-12 19:34 
QuestionNeed some Suggestions. Pin
supriya_rani2012-Sep-11 23:37
membersupriya_rani2012-Sep-11 23:37 
GeneralMy vote of 1 Pin
Member 449609922-Jun-09 20:55
memberMember 449609922-Jun-09 20:55 
GeneralGood Article Pin
khanjee7730-Dec-08 17:30
memberkhanjee7730-Dec-08 17:30 
GeneralMy vote of 2 Pin
Iyigun Cevik8-Dec-08 2:47
memberIyigun Cevik8-Dec-08 2:47 
Generalhii kenny Young Pin
ctcalyx6-Aug-07 21:06
memberctcalyx6-Aug-07 21:06 
GeneralRe: hii kenny Young Pin
Rockrebel7-Aug-08 1:42
memberRockrebel7-Aug-08 1:42 
GeneralRe: hii kenny Young Pin
Kenny Young7-Aug-08 2:26
memberKenny Young7-Aug-08 2:26 
QuestionHyperLinkBitmap Error Pin
diddy4332-Jul-07 6:39
memberdiddy4332-Jul-07 6:39 
GeneralDoens't work with firefox Pin
carlintveld31-May-07 7:50
membercarlintveld31-May-07 7:50 
GeneralRe: Doens't work with firefox Pin
vytas z.16-Aug-07 2:33
membervytas z.16-Aug-07 2:33 
GeneralRe: Doens't work with firefox Pin
bbol19-Nov-07 12:49
memberbbol19-Nov-07 12:49 
GeneralDoes n't Work with ASP.Net 2005 Pin
Hari Darshan26-Apr-07 1:17
memberHari Darshan26-Apr-07 1:17 
QuestionHow to get updated row after drag? Pin
davihigh12-Sep-06 3:29
memberdavihigh12-Sep-06 3:29 
AnswerRe: How to get updated row after drag? Pin
Kenny Young12-Sep-06 7:15
memberKenny Young12-Sep-06 7:15 
QuestionCheckbox. Pin
py1rdj4-Sep-06 11:20
memberpy1rdj4-Sep-06 11:20 
AnswerRe: Checkbox. Pin
Kenny Young12-Sep-06 7:17
memberKenny Young12-Sep-06 7:17 
QuestionDrag n Drop from listbox to a datagrid Pin
kumarcs4-Sep-06 4:11
memberkumarcs4-Sep-06 4:11 
AnswerRe: Drag n Drop from listbox to a datagrid Pin
Kenny Young12-Sep-06 7:21
memberKenny Young12-Sep-06 7:21 
GeneralDatagrid to Datagrid drag drop Pin
sandeepautade31-Aug-06 2:31
membersandeepautade31-Aug-06 2:31 
GeneralRe: Datagrid to Datagrid drag drop Pin
Kenny Young31-Aug-06 2:39
memberKenny Young31-Aug-06 2:39 
GeneralDRAG DROP Pin
Mon217-Aug-06 20:45
memberMon217-Aug-06 20:45 
GeneralRe: DRAG DROP Pin
Kenny Young31-Aug-06 2:37
memberKenny Young31-Aug-06 2:37 
Generalmicrosoft treeview [modified] Pin
azidi13-Jul-06 14:37
memberazidi13-Jul-06 14:37 
GeneralThe DIV does not move Pin
Sebastian_M7-Mar-06 1:07
memberSebastian_M7-Mar-06 1:07 
GeneralRe: The DIV does not move Pin
Sebastian_M7-Mar-06 3:07
memberSebastian_M7-Mar-06 3:07 
GeneralRe: The DIV does not move Pin
Saurabhdotnet23-Mar-06 1:08
memberSaurabhdotnet23-Mar-06 1:08 
GeneralDrag and Drop from Desktop to Web Page Pin
Allan Holbrook15-Feb-06 19:29
memberAllan Holbrook15-Feb-06 19:29 
GeneralRe: Drag and Drop from Desktop to Web Page Pin
Kenny Young16-Feb-06 3:10
memberKenny Young16-Feb-06 3:10 
GeneralGreat!!! Pin
pietro7713-Jan-06 5:42
memberpietro7713-Jan-06 5:42 
GeneralMultiple positions Pin
Judd Williams28-Sep-05 4:37
memberJudd Williams28-Sep-05 4:37 
Generaltextbox Pin
aleks2343-Aug-05 10:39
memberaleks2343-Aug-05 10:39 
GeneralRe: textbox Pin
Kenny Young3-Aug-05 10:52
memberKenny Young3-Aug-05 10:52 
Questiondocument.all(&quot;DW&quot;) - ? Pin
Anonymous20-Jul-05 19:03
sussAnonymous20-Jul-05 19:03 
AnswerRe: document.all(&quot;DW&quot;) - ? Pin
Kenny Young21-Jul-05 2:26
memberKenny Young21-Jul-05 2:26 
GeneralListBox Pin
alandrizh12-Jul-05 6:13
memberalandrizh12-Jul-05 6:13 
GeneralMissing Zip File Pin
sserx7-Mar-05 23:16
membersserx7-Mar-05 23:16 
GeneralRe: Missing Zip File Pin
Kenny Young5-May-05 9:17
memberKenny Young5-May-05 9:17 
QuestionWhat is document.all(&quot;DW&quot;)? Pin
Joe Don Mitchell2-Mar-05 9:07
memberJoe Don Mitchell2-Mar-05 9:07 
AnswerRe: What is document.all(&quot;DW&quot;)? Pin
Kenny Young2-Mar-05 11:52
sussKenny Young2-Mar-05 11:52 
GeneralRe: What is document.all(&quot;DW&quot;)? Pin
Joe Don Mitchell3-Mar-05 4:10
memberJoe Don Mitchell3-Mar-05 4:10 
GeneralUsing Datagrid Pin
Ajit Ober17-Feb-05 8:13
sussAjit Ober17-Feb-05 8:13 
GeneralRe: Using Datagrid Pin
Kenny Young17-Feb-05 8:32
sussKenny Young17-Feb-05 8:32 
GeneralRe: Using Datagrid Pin
Ajit Ober17-Feb-05 9:38
sussAjit Ober17-Feb-05 9:38 
GeneralRe: Using Datagrid Pin
Kenny Young18-Feb-05 9:17
sussKenny Young18-Feb-05 9:17 
GeneralRe: Using Datagrid Pin
Anonymous28-Feb-05 4:07
sussAnonymous28-Feb-05 4:07 
I could'nt even drag and drop some text within a cell of a grid. Am I missing something?

I appreciate your response.

GeneralRe: Using Datagrid Pin
Anonymous28-Feb-05 5:36
sussAnonymous28-Feb-05 5:36 
GeneralRe: Using Datagrid Pin
Ajit Ober28-Feb-05 8:44
sussAjit Ober28-Feb-05 8:44 
GeneralRe: Using Datagrid Pin
Ajit Ober28-Feb-05 8:46
sussAjit Ober28-Feb-05 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 28 Dec 2004
Article Copyright 2004 by Kenny Young
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid