Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm need to get the user Screen Resolution in my SharePoint site application page.

My first approach was using:

int ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

int ScreenWith = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;

but doing this in code behind the System.Windows.Forms.Screen.PrimaryScreen.Bounds doesn't get the correct values from user screen resolution.

In my research I found that using JavaScript is a better approach.

So I create my code like this:

In the application page I create the script:

var width = screen.width; var height = screen.height; if (width ').value = width; document.getElementById('').value = height;

The script is getting the values correctly because when I browse the page I can see the alert message with the correct screen resolution values.

The problem is that when I'm passing the values to code behind as in the code bellow I'm getting always the variables iScreenHeight and iScreenWith with null values:

string sScreenHeight = hScreenHeight.Value; string sScreenWith = hScreenWidth.Value; int iScreenHeight = Convert.ToInt32(sScreenHeight); int iScreenWith = Convert.ToInt32(sScreenWith);

if (iScreenHeight >= 1280 && iScreenWith >= 1024) { lblScreenResolution.Text = "Resolution" + "=" + iScreenHeight + "x" + iScreenWith; lblScreenResolution.ForeColor = Color.Green;

        }
        else {
            lblScreenResolution.Text = "Resolution" + "=" + iScreenHeight + "x" + iScreenWith;
            lblScreenResolution.ForeColor = Color.Yellow;
        }

I think I'm passing correctly the values to the HidenField but I don't know why I'm getting the variables = null when the page loads.

Can any one help me in this code or give me a better approach is exist, I already google it a lot but nothing seems to work?

Thank you

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can do this with native JavaScript by pulling from the screen object. screen.width & screen.height will give you the width and height, respectively.

If you've got jQuery loaded, you can also use $(window).height() and $(window).width() for the same thing, but this is one of those cases where simple core JavaScript makes more sense unless you already have jQuery loaded. jQuery will do some cross-browser compatibility to make sure you're getting accurate readings across all browsers, whereas the core JavaScript approach will sometimes be inconsistent.

That said, if all you're trying to do is do some rudimentary resolution detection, the core stuff should be sufficient.

share|improve this answer
    
Thank you for you help, but I have one question? After get the screen.width and screen.height how I pass the value to the code behind to out my logic? I tried using HidenFields but the values are always null. Can you give me a code example? Thank You –  marinho Sep 4 '12 at 13:02
    
You have to store it in a cookie or pass it via ajax once read, or store into a hidden input somewhere for post requests etc. –  Hugh Wood Sep 4 '12 at 13:15
    
The approach from @HughWood above would work. You can't simply pass from front-end to back-end because the code behind executes pre-render and the JavaScript executes post-render. That's the core difference between server side and client side. You can write from server side to the client, but not from the client to the server. You'd have to capture/store then do a post back to load it (using one of those methods). What are you trying to do? I'd imagine there's a better way than this. –  webdes03 Sep 4 '12 at 13:25
    
I think you get the point "webdes03", but can you give me one example how I do a postback to load it is asp.net without the use of one button? –  marinho Sep 4 '12 at 13:30
    
I would use $("document").ready(function() {...}); and in there call an ajax function which performs a get request to a custom handler that stores it in the location you require. If you need the information for the page you are looking at then the ONLY reason I forsee you needing it can be manipulated in real time with jquery/javascript. With these two methods you can either do an automated post back or cut out the need for one at all. –  Hugh Wood Sep 4 '12 at 13:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.