0
protected void lbkShowWork_Click(object sender, EventArgs e)
{
}

How do I get the position of the mouse when I click the LinkButton?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
willsonchan
  • 200
  • 2
  • 14

2 Answers2

1

Short answer: you can't. ASP.NET WebForm's events are an abstraction on top of the HTTP <form> POST action. Web browsers don't submit the coordinates of the cursor.

Long answer: it's possible, using a variety of means depending on your scenario. The "easiest" way is to use a framework like jQuery and handle client-side mouse-click events for every element on the page, when the mouse is clicked you intercept the form's onsubmit action and update an <input type="hidden" /> with the coordinates and then let the form submission continue.

...or use an <input type="image" /> which includes the coordinates when submitted http://dev.w3.org/html5/markup/input.image.html

Of course, your code makes an assumption that the user is using a mouse to begin with. There are other web user-agents that don't have pointing devices, like smartphones, touch-tablets, and spiders.

Dai
  • 141,631
  • 28
  • 261
  • 374
1

Capture the mouse position from client side (as mentioned in the previous answer) at the mousemove event and keep in a hidden field.

$(document).ready(function () {
    $(document).mousemove(function (event) {
        var currentPos = "X:" + event.pageX;
        currentPos += ",Y:" + event.pageY;
        $("#hdMousePosition").val(currentPos);
    });            
});
Jith
  • 1,701
  • 3
  • 16
  • 22