location (co ordinates )of a control

Last post 08-07-2009 4:02 AM by mendhak. 2 replies.

Sort Posts:

  • location (co ordinates )of a control

    08-04-2009, 3:54 AM
    • Member
      30 point Member
    • shankbond
    • Member since 06-04-2009, 4:59 AM
    • Posts 147

    Hi,

    I am making a web form in asp.net?/c#, I want to know how to know the co ordinates; the x, y coordinates ( in pixels) of a control and also if possible what is the length and breadth( in pixels) of my form.

    thanks

    Shankbond

    the next "gates"
  • Re: location (co ordinates )of a control

    08-07-2009, 3:49 AM
    Answer

    Hi shankbond,

    In this case, we can use JavaScript to achieve the goal:

    <%@ Page Language="C#" %>
    
    <script runat="server">  
      
     
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head >
    <title>Test Page</title>
    
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="TestButton" />
    </form>
    <script type="text/javascript">
        window.onload = function() {
        var btn = document.getElementById("Button1");
            alert("X:"+btn.clientLeft+"\nY:"+btn.clientTop+"\npage Width:"+document.body.clientWidth+"\npage Height:"+document.body.clientHeight);
        }
    </script>
    </body>
    </html>
    


     

    Forward Sun
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: location (co ordinates )of a control

    08-07-2009, 4:02 AM
    Answer
    • All-Star
      16,412 point All-Star
    • mendhak
    • Member since 05-28-2004, 6:58 PM
    • 51.507991,-0.127784
    • Posts 2,611

    To get the X position:


    function GetXPosition(elem)
      {
        var currentLeft = 0;
        if(elem.offsetParent)
            while(1==1) 
            {
              currentLeft = currentLeft + elem.offsetLeft;
              if(!elem.offsetParent)
                {
                   break;
                }
              elem = elem.offsetParent;
            }
        else if(elem.x)
            {
               currentLeft = currentLeft + elem.x;
            }
        return currentLeft;
      }
    


    To get the Y position


    function GetYPosition(elem)
      {
        var currentTop = 0;
        if(elem.offsetParent)
            while(1==1) 
            {
              currentTop = currentTop + elem.offsetTop;
              if(!elem.offsetParent)
                {
                   break;
                }
              elem = elem.offsetParent;
            }
        else if(elem.y)
            {
               currentTop = currentTop + elem.y;
            }
        return currentTop;
      }
    

    To get the viewport dimensions, use window.innerWidth and window.innerHeight. But if it's IE6, then you'll need to use document.documentElement.clientWidth.

    I bought a movie from the store the other day. It had a rating of 3.142 stars out of 5. Turns out it was a pi rated DVD.
Page 1 of 1 (3 items)