Results 1 to 8 of 8

Thread: JavaScript getClientRects() does not work in Content Page

  1. #1
    PowerPoster
    Join Date
    Feb 02
    Location
    Canada, Toronto
    Posts
    5,691

    JavaScript getClientRects() does not work in Content Page

    I have a aspx page with a table with scrollable rows.
    I achived this by having a "header" table, and a "rows" table. The rows table has a div with "overflow:auto" so get the scroll bar.

    When data is loaded in the rows table, the columns have diferent sizes, and since the columns are not fixed width, the header columns don't match with rows columns.

    To fix this, using JavaScript, I loop through the first row in the "rows" table, and I call the getClientRects() function to get the cell bownds, calculate the column width and assign that value to the header table columns.

    This works perfectly when I use a regular aspx page. I decided to switch to a master-content achitecture, because I have a tab menu at the top of the page, and I copied and pasted the code into the content page.

    Everything works fine, but the getClientRects() returns all values as 0 (zeros).


    So how can I get the getClientRects() to return the propper values ?



    Here is the relevant code:
    ASP Code:
    1. <script language="JavaScript" type="text/javascript">
    2.     function SetTableColWidth() {
    3.         var th = document.getElementById("tbl_SearchResult_Header");
    4.         var tr = document.getElementById("tbl_SearchResult_Rows");
    5.  
    6.         if (tr != null) {
    7.             if (tr.rows.length > 0) {
    8.                 for (i = 0; i < 7; i++) {
    9.                     rcts = tr.rows[0].children[i].getClientRects();
    10.                     th.rows[1].children[i].style.width = (rcts[0].right - rcts[0].left) + "px";
    11.                 }
    12.  
    13.                 for (i = 0; i < 7; i++) {
    14.                     rcts = th.rows[1].children[i].getClientRects();
    15.                     tr.rows[0].children[i].style.width = (rcts[0].right - rcts[0].left) + "px";
    16.                 }
    17.             }
    18.         }
    19.     }
    20. </script>
    21.  
    22.  
    23. <table id="tbl_SearchResult_Header" cellpadding="2" cellspacing="1" width="750px" style="background: #99B4F1; padding: 0px; border-style: solid solid none solid; border-width: 2px; border-color: #000000">
    24. <tr class="SectionTableTitle">
    25.     <td colspan="7">Search results</td>
    26. </tr>
    27. <tr class="SectionTableTitle">
    28.     <td>aaaaaaaaaa</td>
    29.     <td>aaaaaaaaaa</td>
    30.     <td>aaaaaaaaaa</td>
    31.     <td>aaaaaaaaaa</td>
    32.     <td>aaaaaaaaaa</td>
    33.     <td>aaaaaaaaaa</td>
    34.     <td>aaaaaaaaaa</td>
    35. </tr>
    36. </table>
    37. <div style="width:770px;height:255px;overflow:auto;">
    38. <asp:Repeater ID="rptSearchResults" runat="server">
    39. <HeaderTemplate>
    40.     <table id="tbl_SearchResult_Rows" cellpadding="2" cellspacing="1" width="750px" style="background: #99B4F1; padding: 0px; border-style: none solid solid solid; border-width: 2px; border-color: #000000">
    41. </HeaderTemplate>
    42. <ItemTemplate>
    43.     <tr align="left" bgcolor='#E0E0E0'
    44.             onmouseover="this.bgColor='#C8C8C8'" onmouseout="this.bgColor='#E0E0E0'"
    45.             onclick="SubmitChoice(<%#Container.DataItem("aaaaaaaaaa_ID")%>);" style="cursor:pointer;">
    46.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    47.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    48.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    49.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    50.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    51.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    52.         <td align="center"><%#Container.DataItem("aaaaaaaaaa")%></td>
    53.     </tr>
    54. </ItemTemplate>
    55. <AlternatingItemTemplate>
    56.     <tr align="left" bgcolor='#F0F0F0'
    57.             onmouseover="this.bgColor='#C8C8C8'" onmouseout="this.bgColor='#F0F0F0'"
    58.             onclick="SubmitChoice(<%#Container.DataItem("aaaaaaaaaa_ID")%>);" style="cursor:pointer;">
    59.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    60.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    61.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    62.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    63.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    64.         <td><%#Container.DataItem("aaaaaaaaaa")%></td>
    65.         <td align="center"><%#Container.DataItem("aaaaaaaaaa")%></td>
    66.     </tr>
    67. </AlternatingItemTemplate>
    68. <FooterTemplate>
    69.     </table>
    70. </FooterTemplate>
    71. </asp:Repeater>
    72. </div>
    73.  
    74.  
    75. <script language="JavaScript" type="text/javascript">
    76.         SetTableColWidth();
    77. </script>

    I attached an image to get an idea how it looks (when it works)
    Attached Images Attached Images  

  2. #2
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,519

    Re: JavaScript getClientRects() does not work in Content Page

    Hi.Read this:
    http://www.codeproject.com/Articles/...-getElementByI

    Also if you don't have a specification on having everything in one row you could css your columns with a specified width so they will go on the next line when end of cell is reached or you can specify them to cut the word if they reach the end of the cell.
    Slow as hell.

  3. #3
    PowerPoster
    Join Date
    Feb 02
    Location
    Canada, Toronto
    Posts
    5,691

    Re: JavaScript getClientRects() does not work in Content Page

    Thanks for your reply, but...

    getElementById returns the objects of both tables... I debugged the app, and the values are there...

    The problem is with getClientRects() function returning rects of "0 0 0 0" when the code is in the content page.

    Quote Originally Posted by sapator View Post
    Also if you don't have a specification on having everything in one row you could css your columns with a specified width so they will go on the next line when end of cell is reached or you can specify them to cut the word if they reach the end of the cell.
    So your saying to have the columns fixed length, and have the data roll to next line if it does not fit?

    That is not an option for me when my code works fine in an aspx page, it re-sizes columns just fine with dynamic data, and the header will match the data column sizes.

  4. #4
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,519

    Re: JavaScript getClientRects() does not work in Content Page

    You debugged on the server or on your local PC?Master Page or content page?
    I would suggest to give the values one try just to be sure because even if it works now you may have trouble when server deploying.Won't hurt i guess
    Slow as hell.

  5. #5
    PowerPoster
    Join Date
    Feb 02
    Location
    Canada, Toronto
    Posts
    5,691

    Re: JavaScript getClientRects() does not work in Content Page

    I debugged on my local PC. The code is in the content page.
    Quote Originally Posted by sapator View Post
    I would suggest to give the values one try just to be sure because even if it works now you may have trouble when server deploying.Won't hurt i guess
    I'm not sure what you mean by that?

  6. #6
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,519

    Re: JavaScript getClientRects() does not work in Content Page

    It means that the values you get local may not work when deploying on the actual server.
    Slow as hell.

  7. #7
    PowerPoster
    Join Date
    Feb 02
    Location
    Canada, Toronto
    Posts
    5,691

    Re: JavaScript getClientRects() does not work in Content Page

    I published the web-site to the server, and I get the same results. It works in the regular aspx page, but it does not work in the content page.

    This is how I expected, because the problem is with javascript function, which executes on the client side...

  8. #8
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,519

    Re: JavaScript getClientRects() does not work in Content Page

    Ok my last thought is to try to pass the javascript through the server side:
    http://www.codeproject.com/Articles/...-RegisterStart
    and for getting the placeholders:
    http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx
    Slow as hell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •