Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / XHTML

Capture the mouse movement in web service

2.33/5 (2 votes)
11 May 2008CPOL1 min read 1   191  
Capture the mouse movement though web service using ASP.NET AJAX.

Introduction

Capture the mouse movement though web service using AJAX. The beauty for web service is, you can call the web service through javascript function and you can interact with database. Here i develop simple web page which display a image inside the div tag. If you move this image the latest position will captured by web service and the information will store in the database without page loading..

Using the code

Web service(Service_Position.asmx)
Make a new ajax enabled website in VS 2005. Create a web service with the name 'Service_Position.asmx' . Add the System.Web.Script.Services.ScriptService attribute to the service class. This attribute enables access to the Web service from ASP.NET AJAX client side code. In 'StorePos1' function, am passing 2 parameters x and y.

//
// Service_Position.asmx
//
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class Service_Position
Inherits System.Web.Services.WebService
<WebMethod(EnableSession:=True)> _
Public Function StorePos1(ByVal x, ByVal y) As String
Dim posX As String
Dim posY As String
posX = x
posY = y
'StoreinDB(x, y)
Return "1"
End Function
End Class

Create a file called 'Default.aspx'. Create one div control and one image control.

<div id="lay1" runat="server" class="displayretainer" style=" left: 270px; width: auto; text-indent: 15px; position: absolute; top: 158px; height: auto; text-align: center; cursor:move;" onMouseDown="Move(this,'Img1')">
<asp:Image ID="Img1" ImageUrl="smiley1_tn.gif" runat="server" /> 
</div>

In mousedown event, we are getting the x and y position and send this to web service.

Service_Position.StorePos1(x1,y1); 

In script manager we have to refere the webservice file.

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service_Position.asmx" />
</Services>
</asp:ScriptManager>
thats it. Now move the image object around the screen and check your database how its captured.

Points of Interest

This concept am using for my new project online T-Shirt designer. Its a simple and useful idea.

History

N/A

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)