I'm calculating the screen size of the browser and storing it in a hidden field and i'm dynamically creating controls on the page OnInit() based on the sizes. But OnInit() is getting fired before javascr开发者_如何学Cipt function fires to calculate the screen size.
Any ideas?
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).
try using
Request.Browser.ScreenPixelsHeight
Request.Browser.ScreenPixelsWidth
instead of javascript to find out screen resolution
An easier implementation probably is to redirect the user to an intermediate page (a dummy page which displays "please wait, loading..." kind of message). On this page, you could calculate all your screen size and then pass it over to the actual page you want to resize.
So, you'd redirect to the intermediate page first, calculate the screen size and then load the actual page (that you want to resize) with the screen size info you calculated in the intermediate page.
精彩评论