.net - screen size code behind

Posted on 2013-11-25
Last Modified: 2013-11-29
I am using .net C#. How do I find the size of the screen in code behind?
Question by:johnson1
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 39673956

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 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.
LVL 82

Expert Comment

ID: 39674362
Check this Experts Exchange article :

As smeghammer said, you need to do a redirection
LVL 10

Expert Comment

ID: 39675632
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.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


Author Comment

ID: 39678298
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?

Assisted Solution

smeghammer earned 250 total points
ID: 39678342
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.

LVL 10

Accepted Solution

GeoffSutton earned 250 total points
ID: 39678943
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. 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 :)

Author Closing Comment

ID: 39685036
Thank you.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery keyup 4 19
i have to take the screenshot of command prompt? how to do this? 1 37
How can i  prevent default  Submision? 6 27
VB.NET 2008 - SQL Timeout 9 24
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question