当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - How to get the postion of the mouse during Button OnClick

问题描述:

protected void lbkShowWork_Click(object sender, EventArgs e)

{

}

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

网友答案:

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.

网友答案:

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);
    });            
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: