0

Hi I'm currently working on a asp.net project and I need to make a button which when the user moves the cursor from left to right or right to left a hidden text field is filled and then a dopostback is called for the serverside to read the value of that hidden field.

My problem is that when I create the event listener it doesn't call, thus the pageX does not work. I'm currently testing on Google chrome browser, I've tested the clientX part in the IE and works well.

This is the .aspx side of the cording

<asp:Button ID="Button2" runat="server" Text="<<<------          ------>>>" 
  Width="50%" Height="60px" onclick="Button2_Click" 
  onmouseover="getMouseXY()" onmouseout="getMouseOut()"/>   


  <script type="text/javascript">
        var IE = document.all?true:false;
        var dayD = 0;
        var inCordX = 0;
        var outCordX = 0;                

        if (!IE) 
        document.captureEvents(Event.MOUSEMOVE)                 

        function getMouseOut(e){    
            if (IE) {outCordX = event.clientX + document.body.scrollLeft;}
            else {outCordX = e.pageX;}                                                   
            if(inCordX>outCordX){dayD=1;}else{dayD=-1;}           
            document.getElementById("outputResult").value = dayD;                        
            __doPostBack("","");
        }       

        function getMouseXY(e) {        
            if (IE) {inCordX = event.clientX + document.body.scrollLeft;}
            else {inCordX = e.pageX;}              
            alert(inCordX);
            return true;
        }
    </script>

This is the coding aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {            
        if ((Request.Form["outputResult"] == "-1")
           ||(Request.Form["outputResult"] == "-1"))
        {
            Button2_Click(this, new EventArgs());
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Button2.Text = Request.Form["outputResult"] + "";
    }
sgrech
  • 35
  • 8
  • 1
    So this does not work in Chrome only? Have you [debugged](http://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome/66434#66434) it? Do you get any js-errors? – Tim Schmelter Jun 11 '11 at 21:49
  • I'm getting this error in chrome "Cannot read property 'pageX' of undefined" – sgrech Jun 11 '11 at 22:08

1 Answers1

0

Not so sure, but try

  onmouseover="getMouseXY" onmouseout="getMouseOut"/> 

instead of

  onmouseover="getMouseXY()" onmouseout="getMouseOut()"/> 
ixth
  • 78
  • 7
  • I had tried that. The methods will not get called. I think the problem is in the "document.captureEvents(Event.MOUSEMOVE)" I don't know how to add the Event, since that the current page is a Content Page of a master page. – sgrech Jun 11 '11 at 23:19
  • Not sure about ASP, but seems like there is no crossbrowser way to get event object, when you use early events handlers. http://www.quirksmode.org/js/events_early.html But I'm not sure about ASP. Maybe it fundamentally modify js environment. – ixth Jun 11 '11 at 23:46
  • Thanks ixth for that link. I realized that I could pass the event when the function was being called. My problem was that the "document.captureEvents(Event.MOUSEMOVE)" was not passing the event. I simply had to call the functions "getMouseXY(event)" rather than "getMouseXY()". – sgrech Jun 12 '11 at 00:28