简体   繁体   English

获取相对于OnClick事件中控件的鼠标单击位置

[英]Get the mouse-clicked position relative to control within OnClick event

I have this bulletedlist: 我有这个项目符号清单:

<asp:BulletedList ID="BLLinks" runat="server" Width="100%" 
BulletStyle="CustomImage" BulletImageUrl="~/Images/link.png" 
OnClick="BLLinks_Click" ></asp:BulletedList>

I need to get the position relative to the bulletedlist item where the user clicked in this event: 我需要获得相对于用户在此事件中单击的项目符号列表项的位置:

protected void BLLinks_Click(object sender, BulletedListEventArgs e)
{
   // check the clicked position in the bulleted list
   // if on the bulleted list image do so and so...
   // if on the bulleted list item text do so and so....

}

You can get the mouse position with client code and store it in a hidden field. 您可以使用客户端代码获取鼠标位置,并将其存储在隐藏字段中。

Here is the markup: 这是标记:

<asp:HiddenField ID="hiddenMouseXY" runat="server" />
<asp:BulletedList onmousedown="bulletedListMouseDown(this, event);" ... >
    ...
</asp:BulletedList>

And the Javascript function: 和Javascript函数:

function bulletedListMouseDown(lst, evt) {
    var x = evt.pageX - lst.offsetLeft;
    var y = evt.pageY - lst.offsetTop; 
    var coord = "X: " + x + ", Y: " + y;
    alert(coord);
    document.getElementById('hiddenMouseXY').value = coord;
}

If you want the click to trigger a postback, you can call __doPostBack in the Javascript function. 如果希望单击触发回发,则可以在Javascript函数中调用__doPostBack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM