Click here to Skip to main content
15,663,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am practicing a small project. According to mouse movements, the stop watch has to be started or stop.
ie, once the mouse is inside then start the stopwatch otherwise stop. Is it possible?
my codes: aspx page
<div id="Divn1" onmousemove="MyNewFunction(event)" onmouseout="ClearCoor()" style="width:99%; height:99%; border:solid;background-color:white; position:absolute;">
            <p id="Demo1"></p>
            <asp:Label ID="Label2" runat="server" Font-Size="XX-Large"></asp:Label>
            <div id="Divn2" style="width:50px;height:50px;border-radius:50%;background-color:brown; position:absolute; top:45%; margin-left:47%;">

           <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label>
                <asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" />
                <asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />


And then aspx.cs
public static Stopwatch sw;
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            sw = new Stopwatch();
    protected void tm1_Tick(object sender, EventArgs e)
        long milsecs = sw.Elapsed.Milliseconds;
        if (Label2.Text.Trim().Length > 0)
            Label1.Text = DateTime.Now.ToString("00:ff");
        //Label1.Visible = false;


What I have tried:

Tried to start watch while the mouse moving
Updated 12-Jun-17 2:53am
F-ES Sitecore 12-Jun-17 4:20am    
Server code isn't really suitable for this kind of task, you're better doing this in javascript. Also your use of "static" to get the Stopwatch working means that everyone who uses your site has the same Stopwatch and they'll be starting\stopping each others meaning you don't get the result you want. It might work when it is just you using the site but it won't work for others.

1 solution

No. It is not possible that way...
In ASP.NET server and client are so separated, that server does not know about mouse position on client nothing, so you can not use it as indicator...
You should learn client side (JavaScript) programming to create a stopwatch there and stop/start it on client side events (hover) of the label...
Other option is to find an other Timer class (not the one from Microsoft) with client side capabilities...
Share this answer
Paramu1973 13-Jun-17 18:29pm    
Any Sample Codes or links? Thanks
Kornfeld Eliyahu Peter 14-Jun-17 2:15am    
There probably hundred of thousands - google them...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900