Click here to Skip to main content
15,867,568 members
Articles / Web Development / XHTML
Article

Capture the mouse movement in web service

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
11 May 2008CPOL1 min read 26.9K   191   18   1
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)


Written By
Software Developer Dubai based company
United Arab Emirates United Arab Emirates
I like to find out new technology in web.

Comments and Discussions

 
GeneralGood idea Pin
CoperNick16-Mar-10 21:12
CoperNick16-Mar-10 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.