.net - screen size code behind

I am using .net C#. How do I find the size of the screen in code behind?
Who is Participating?
I completely agree.  While it is possible to get browser data on the server side, what you are doing is asking your wife to hand you the phone so that you can call your parents and ask them to call your wife to give her a message :)  A very roundabout method (which works... but is clumsy).

You can add an even listener for the javascript event "Resize" where you can detect the width and height of your window, then use those to assign css classes to your objects, or to set positioning.  http://css-tricks.com has some great layout functions available through CSS and well writing information on how to use them.  As a general adage, if you can do it on the client without a postback, then do it on the client.  The more you can do on the client the quicker your site will respond.  
If you need an async style function on the client, use setTimeout(function(){...},0) to put the event into a pseudo separate thread (not perfect, but the closest we can get in javascript, and it doens't cause event blocking in long running functions).

Smeghammer is 100% right :)

Don't forget the separation of server-side (C#) anc client-side (javascript)...

The code-behind runs at the server, so will have no idea about the client browser dimensions unless you explicitly send this information back to the webserver AFTER the first request has completed. What you will need to do is post back the screen width/height as determined by a little javascript (e.g. see http://www.w3schools.com/js/js_window.asp). You can then post this back (traditional form field or AJAX) and have the server-side C# do whatever with the values.

However, I think the main quesion is what are you trying to do? It may well be that what you are needing would be better addressed by client-side javascript anyway.
Check this Experts Exchange article :

As smeghammer said, you need to do a redirection
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Try using Request.Browser.ScreenPixelsWidth and Request.Browser.ScreenPixelsHeight.  That will give you what you want without having to mess with intermediary code.  You can also use the Request.Browser to get a plethora of information about what you really want to know.
johnson1Author Commented:
Thank you for your answers. Snaghammer asked what I was doing with this. I am creating the webpage dynamically. I will detect the size and arrange the items according to the size as the control I am using has not responsive design. Do you think this is worse approach then doing this on the client? Can you please tell me what happens when the user rotates for example an iphone. The width of the screen becomes larger and height less, but do you know how to detect the rotation so that I can do a postback with the new screen width and height?
If you can do this on the client, it will be much better as the requests back to the server will be much reduced.

Imagine how frustrating it would be to a user if they simply simply rotated their tablet and had to wait while the webpage made a request back to the server - it would also add un-necessarily to their data usage (if a smartphone)... I can also imagine that on a standard desktop, someone simply resizing their browser window would trigger the same kind of thing.

What does the control render in terms of HTML? You will have lots of control over this - hopefully - with client-side CSS and JS if you need to alter how it is presented in the browser.

I would definitely look at doing this formatting client-side as it will remove a load of grief in the future I think.

johnson1Author Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.