Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


.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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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

604 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