Solved

.net - screen size code behind

Posted on 2013-11-25
7
1,508 Views
Last Modified: 2013-11-29
Hello,
I am using .net C#. How do I find the size of the screen in code behind?
0
Comment
Question by:johnson1
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 4

Expert Comment

by:smeghammer
ID: 39673956
Hi,

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

Expert Comment

by:leakim971
ID: 39674362
Check this Experts Exchange article :
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_2298-ASP-NET-Mobile-Device-Detection-and-Redirection.html

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

Expert Comment

by:GeoffSutton
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:johnson1
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?
0
 
LVL 4

Assisted Solution

by:smeghammer
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.

Cheers
0
 
LVL 10

Accepted Solution

by:
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.  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 :)
0
 

Author Closing Comment

by:johnson1
ID: 39685036
Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now