This is a migrated thread and some comments may be shown as answers.

Get RadGrid width

6 Answers 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 23 Feb 2011, 11:18 PM
Hi all,

I set width="100%"in the aspx
And  would like to know its width in pixel after data is filled.
I tried myRadGrid.width, but it just show 100% 
Could someone help me?

Thanks.

Andy.  

6 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 24 Feb 2011, 04:57 PM
Hi Andy,

When you set the width to 100%, the grid is resized to fill its parent container.

So in server-side you do not know what will be the actual width of the grid. For example if you have only one grid in the page when you resize the browser window the grid width will be changed.

You can get the width client-side. Like in the example bellow:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
  <script type="text/javascript" language="javascript">
    function clientClick() {
      var width = $get("<%=RadGrid1.ClientID%>").clientWidth;
      alert(width);
    }
  </script>
</telerik:RadCodeBlock>
<div>
  <input id="btnClient" type="button" value="Check the width" onclick="clientClick();" />
  <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
    ........
  </telerik:RadGrid>

Kind regards,
Vasil
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Andy
Top achievements
Rank 1
answered on 25 Feb 2011, 11:08 PM
Very useful answer 
Thanks Vasil!

ANdy.
0
Andy
Top achievements
Rank 1
answered on 01 Mar 2011, 06:32 PM
Hi Vasil,

I would like to know the RadGrid width on the page load.
So I use 

window.onload = function () {
                var width = $get("<%=myGrid.ClientID%>").clientWidth;
                var mydiv = document.getElementById("mydiv");
                alert(width);
                mydiv.style.marginLeft = Math.abs((980 - width) / 2);
                alert("Result = " + Math.abs((980 - width) / 2));
            }

It's good on IE6.
But I can't get the width on IE8.

Thanks,

Andy.
0
Vasil
Telerik team
answered on 02 Mar 2011, 02:44 PM
Hi Andy,

I get a similar result here except that it works on IE8, but not work on IE7 (it alert 0 width).
Using offsetWidth property instead of clientWidth will fix this problem.

Best wishes,
Vasil
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Hung Ha
Top achievements
Rank 1
answered on 02 Mar 2011, 04:40 PM
Hi Vasil,

Like a charm, you solve the problem.
However, it just works in IE, not in Chrome, or Firefox

Thanks.

0
Vasil
Telerik team
answered on 07 Mar 2011, 12:34 PM
Hi,

Using offsetWidth should work on all browsers. If you use it inside another function, please check that all other properties that you are using are also avaiable in Firefox.

Kind regards,
Vasil
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Andy
Top achievements
Rank 1
Hung Ha
Top achievements
Rank 1
Share this question
or