The OnInit
method you refer to seems to me the OnInit
method in ASP.NET's page loading cycle. Since JavaScript is client-side (i.e., it runs on the browser) and C# / ASP.NET is server-side (i.e., it runs on the server, inside IIS), it will always be triggered prior to any javascript function runs.
The approach you have chosen requires two cycles: first the page is requested and you can set the hidden fields with your JavaScript method. This page is then auto-posted-back to the server and you calculate the heights and widths of your items.
However, this approach will give you a lot of headaches, now and in the future. It is often better to solve layout problems that depend on the browser's viewport (the size you talk of) solely on the client side. An excellent start is CSS. If CSS can't solve it, use jQuery (or alternatives).