Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


.net - screen size code behind

Posted on 2013-11-25
Medium Priority
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 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.
LVL 83

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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 1000 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 1000 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.  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 :)

Author Closing Comment

ID: 39685036
Thank you.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

572 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