Click here to Skip to main content
Click here to Skip to main content
Go to top

ASP.NET - Drag and Drop on a Webpage

, 28 Dec 2004
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>

var TimerID = 0;
var oEl     = null;
var oTarget = null;
var beginDrag = false;
var tmpHTML = "";
function killTimer()
    if (TimerID != 0 )
        TimerID = 0;
function fnShowDragWindow() 
    var obj = document.all("DW");
    if (oEl == null)  
    }        = oEl.offsetTop;        = oEl.offsetLeft;    = oEl.offsetHeight - 3;        = oEl.offsetWidth - 3;
    obj.innerText        = oEl.SpecimenId;    = "block";    = 999;
    window.document.attachEvent( "onmousemove"  , fnMove );
    window.document.attachEvent( "onscroll"  , fnMove );
    window.document.attachEvent( "onmousemove" , fnCheckState );
    window.document.attachEvent( "onmouseup"    , fnRelease );
    window.document.attachEvent( "onselectstart", fnSelect );
    beginDrag = true;

function setTarget(id)
    var src = document.getElementById(id);
    if (src == null) 
    if ( == 'true')
        oTarget = src;
        oTarget = null;    

function BeginDrag(id)
    // Get the item to be dragged.
    oEl = document.getElementById(id);
    // Is there an item?
    if(oEl == null)
    tmpHTML = oEl.innerHTML;
    // Set the window timeout.
    TimerID = setTimeout(fnShowDragWindow, 1);

function fnCheckState()
    if(event.button != 1)

function fnSelect()
    return false;

function fnMove()
    if (event.button != 1)
    var obj = document.all("DW"); = 
      event.clientY - (obj.offsetHeight/2) + window.document.body.scrollTop; = event.clientX  + window.document.body.scrollLeft;
    window.status = 'Top=' + + ' Left=' +;
    if (event.clientY >

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

Comments and Discussions

GeneralRe: Missing Zip File PinmemberKenny Young5-May-05 9:17 
QuestionWhat is document.all(&quot;DW&quot;)? PinmemberJoe Don Mitchell2-Mar-05 9:07 
AnswerRe: What is document.all(&quot;DW&quot;)? PinsussKenny Young2-Mar-05 11:52 
GeneralRe: What is document.all(&quot;DW&quot;)? PinmemberJoe Don Mitchell3-Mar-05 4:10 
GeneralUsing Datagrid PinsussAjit Ober17-Feb-05 8:13 
GeneralRe: Using Datagrid PinsussKenny Young17-Feb-05 8:32 
GeneralRe: Using Datagrid PinsussAjit Ober17-Feb-05 9:38 
GeneralRe: Using Datagrid PinsussKenny Young18-Feb-05 9:17 
To drag and drop images, the mousemove handler must return false. Someone posted a question about dragging a dropping images and I replied with a code example. The question is one or two below yours.
First, try dragging and dropping some text in the cell of a grid and see if that works. If it does, then ensure the mousemove handlers return false.
GeneralRe: Using Datagrid PinsussAnonymous28-Feb-05 4:07 
GeneralRe: Using Datagrid PinsussAnonymous28-Feb-05 5:36 
GeneralRe: Using Datagrid PinsussAjit Ober28-Feb-05 8:44 
GeneralRe: Using Datagrid PinsussAjit Ober28-Feb-05 8:46 
QuestionFirefox? PinmemberIvo Silvestri14-Feb-05 23:51 
AnswerRe: Firefox? PinsussKenny Young15-Feb-05 2:28 
GeneralRe: Firefox? Pinsusswilliaml22-Feb-05 12:34 
GeneralImage in TD Pinmemberarobadol6-Jan-05 1:28 
GeneralRe: Image in TD PinsussKenny Young7-Jan-05 8:20 
QuestionOnDrop Event? PinmemberFrEaK_CH28-Dec-04 10:09 
AnswerRe: OnDrop Event? PinsussKenny Young28-Dec-04 10:25 
GeneralRe: OnDrop Event? PinmemberFrEaK_CH28-Dec-04 11:58 
GeneralRe: OnDrop Event? PinsussKenny Young29-Dec-04 2:47 

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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 28 Dec 2004
Article Copyright 2004 by Kenny Young
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid