Introduction
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
- Create a web application within your solution.
- Create a web page.
- Add the provided JavaScript in the
<HEAD>
of your <HTML>
.
- Add two textboxes and a button in the web form.
- Add the
DrawContainers
method to the code-behind of the web page.
- Ensure that the
click
event for the button calls the DrawContainers
method.
or
- Download the source code and run the sample application.
JavaScript
This is the JavaScript that enables the drag and drop to take place:
<!---->
var TimerID = 0;
var oEl = null;
var oTarget = null;
var beginDrag = false;
var tmpHTML = "";
function killTimer()
{
if (TimerID != 0 )
{
clearTimeout(TimerID);
TimerID = 0;
}
}
function fnShowDragWindow()
{
var obj = document.all("DW");
killTimer();
if (oEl == null)
{
return;
}
obj.style.top = oEl.offsetTop;
obj.style.left = oEl.offsetLeft;
obj.style.height = oEl.offsetHeight - 3;
obj.style.width = oEl.offsetWidth - 3;
obj.innerText = oEl.SpecimenId;
obj.style.display = "block";
obj.style.zIndex = 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 );
//window.document.attachEvent("onmouseover",setTarget);
beginDrag = true;
}
function setTarget(id)
{
var src = document.getElementById(id);
if (src == null)
{
return;
}
if (src.target == 'true')
{
oTarget = src;
}
else
{
oTarget = null;
}
}
function BeginDrag(id)
{
// Get the item to be dragged.
oEl = document.getElementById(id);
// Is there an item?
if(oEl == null)
{
return;
}
tmpHTML = oEl.innerHTML;
// Set the window timeout.
TimerID = setTimeout(fnShowDragWindow, 1);
}
function fnCheckState()
{
if(event.button != 1)
{
fnRelease();
}
}
function fnSelect()
{
return false;
}
function fnMove()
{
if (event.button != 1)
{
fnRelease();
return;
}
var obj = document.all("DW");
obj.style.top =
event.clientY - (obj.offsetHeight/2) + window.document.body.scrollTop;
obj.style.left = event.clientX + window.document.body.scrollLeft;
window.status = 'Top=' + obj.style.top + ' Left=' + obj.style.left;
if (event.clientY > window.document.body.clientHeight - 10 )
{
window.scrollBy(0, 10);
}
else if (event.clientY < 10)
{
window.scrollBy(event.clientX, -10);
}
}
function fnRelease()
{
if (beginDrag == false) return;
window.document.detachEvent( "onmousemove" , fnMove );
window.document.detachEvent( "onscroll" , fnMove );
window.document.detachEvent( "onmousemove" , fnCheckState );
window.document.detachEvent( "onmouseup" , fnRelease );
window.document.detachEvent( "onselectstart", fnSelect );
//window.document.detachEvent( "onmouseover", setTarget );
var obj = document.all("DW");
if (oTarget != null)
{
var targetHTML = oTarget.innerHTML;
var targetSpecId = oTarget.SpecimenId;
var sourceSpecId = oEl.SpecimenId;
oEl.innerHTML = targetHTML;
oEl.SpecimenId = targetSpecId;
oTarget.SpecimenId = sourceSpecId;
oTarget.innerHTML = tmpHTML;
// Is the container empty?
if(oTarget.innerHTML != "")
{
oTarget.style.backgroundColor="beige";
}
else
{
oTarget.style.backgroundColor = "turquoise";
}
if(oEl.innerHTML != "")
{
oEl.style.backgroundColor = "beige"
}
else
{
oEl.style.backgroundColor = "turquoise"
}
}
killTimer();
obj.style.display = "none";
oEl = null;
oTarget = null;
beginDrag = false;
TimerID = 0;
}
function CancelDrag()
{
if (beginDrag == false)
{
killTimer();
}
}
<!--------- End of Drag and Drop JavaScript ------------>
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;
if(this.txtContX.Text == null || this.txtContY.Text == null)
return;
int rowSize = Int32.Parse(this.txtContX.Text);
int rowNumber = Int32.Parse(this.txtContY.Text);
int numberOfContainers = rowSize * rowNumber;
bool isEmpty = false;
for(int i=0; i< numberOfContainers; i++)
{
int newRow = i % rowSize;
if(tr == null || newRow == 0)
{
tr = new TableRow();
trCol.Add(tr);
}
if((i+1)%17==0)
{
isEmpty = true;
}
else
{
isEmpty = false;
}
TableCellCollection tdc = tr.Cells;
TableCell td = new TableCell();
td.ID = "cell_" + i.ToString();
td.BackColor = Color.Turquoise;
td.CssClass = "SpecimenLoc";
td.Attributes.Add("SpecimenId", "");
if(!isEmpty)
{
td.Attributes.Add("SpecimenId", i.ToString());
td.BackColor = Color.Beige;
td.Text = i.ToString();
}
td.Attributes.Add("target", "true");
td.Attributes.Add("onmousedown", "BeginDrag(this.id);");
td.Attributes.Add("onmouseup", "CancelDrag();");
td.Attributes.Add("onmouseover",
"setTarget(this.id);this.style.cursor='hand';");
td.Attributes.Add("onmouseout",
"this.style.cursor='default';");
td.Width = Unit.Pixel(35);
td.Height = Unit.Pixel(35);
tdc.Add(td);
}
}
C# code behind - button click event
The click
event of the button:
private void btnDrawContainers_Click(object sender, System.EventArgs e)
{
this.DrawContainers();
}