Re: Get mouse position in control.

Tech-Archive recommends: Speed Up your PC by fixing your registry



Any idea why I am getting "Input string was not in a correct format." When
it gets to "panelX As Integer = Int32.Parse(Request.Form("hidX"))"?

"Shawn" <sscamner@xxxxxxxxxxxxx> wrote in message
news:us%23RXN$UGHA.5884@xxxxxxxxxxxxxxxxxxxxxxx
Thanks, I will look it over.

"Jason Hales" <jason_hales@xxxxxxxxx> wrote in message
news:1143713140.525974.7630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You need to handle the onmousemove and onclick event of your panel in
JavaScript.

I've attached an example in C# (should be easy to convert to VB.net).
the getPanelMouseCoOrds JavasScript sets two text boxes (so you can see
it in action) and two hidden fields with the mouse postion.

The onclick handler just submits the form.

When the form is posted back to server the PageLoad extracts the values
from the two hidden form fields and sets the label for info

It works in IE but may ntyo work in other browsers:
Webform1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PanelMousePosition.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<INPUT id="hidX" name="hidX" type="hidden"> <INPUT id="hidY"
name="hidY" type="hidden">
<asp:Panel id="Panel1" runat="server" Width="423px" Height="94px"
BorderStyle="Solid">Panel</asp:Panel>
<p>X: <input name="xPos" type="text" size="5"></p>
<p>Y: <input name="yPos" type="text" size="5"></p>
<asp:Label id="Label1" runat="server"></asp:Label>
<script language="JavaScript">
function getPanelMouseCoOrds()
{
var mouseX = event.clientX + document.body.scrollLeft;
var mouseY = event.clientY + document.body.scrollTop;

document.Form1.xPos.value = mouseX;
document.Form1.yPos.value = mouseY;

document.Form1.hidX.value = mouseX;
document.Form1.hidY.value = mouseY;
return true;
}

function handlePanelClick()
{
document.Form1.submit();
}
</script>
</form>
</body>
</HTML>

and there's the code behind to register and handle events
(WebForm1.aspx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PanelMousePosition
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Panel Panel1;

private void Page_Load(object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Panel1.Attributes["onmousemove"]="getPanelMouseCoOrds()";
Panel1.Attributes["onclick"]="handlePanelClick()";
}
else
{
int panelX = Int32.Parse(Request.Form["hidX"]);
int panelY = Int32.Parse(Request.Form["hidY"]);

Label1.Text = "You clicked @ " + panelX + ", " + panelY;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}





.



Relevant Pages

  • Re: Get mouse position in control.
    ... You need to handle the onmousemove and onclick event of your panel in ... var mouseY = event.clientY + document.body.scrollTop; ... private void Page_Load ... This call is required by the ASP.NET Web Form Designer. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Get mouse position in control.
    ... the getPanelMouseCoOrds JavasScript sets two text boxes (so you can see ... var mouseY = event.clientY + document.body.scrollTop; ... private void Page_Load ... This call is required by the ASP.NET Web Form Designer. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Session Variables Disappearing
    ... This call is required by the ASP.NET Web Form Designer. ... > /// Required method for Designer support - do not modify> ... > private void InitializeComponent() ... > Auth Authentication = new Auth; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Session Variables Disappearing
    ... This call is required by the ASP.NET Web Form Designer. ... > /// Required method for Designer support - do not modify> ... > private void InitializeComponent() ... > Auth Authentication = new Auth; ...
    (microsoft.public.dotnet.framework.aspnet)
  • ASP Net starter pack - MSSQL Server problems so cant finsih tutorials.
    ... Not associated with a trusted SQL Server ... private void Page_Load ... //Bind Product data to Datagrid ... This call is required by the ASP.NET Web Form Designer. ...
    (microsoft.public.dotnet.framework.aspnet)