[RESOLVED]How can i get resolution os visitor?

Hi dear ,

I want get resolution of user and set it in a session but i dont any result

my code in _layout.cshtml is :

<body dir="rtl">
    <script type="text/javascript">

        $(document).ready(function () {
            PageMethods.setResolution(window.innerWidth, window.innerHeight);



and basepage.cs in App_Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.Services;
using System.Web.Script.Services;

/// <summary>
/// Summary description for basepage
/// </summary>
public class basepage : System.Web.UI.Page
	public basepage()
		// TODO: Add constructor logic here

    public static Size ScreenResolution
            return (Size)HttpContext.Current.Session["ScreenResolution"];


    [WebMethod, ScriptMethod]
    public static void setResolution(int width, int height)
        ScreenResolution = new Size(width, height);

after run project HttpContext.Current.Session["ScreenResolution"] is empty .why?

please help me


To avoid any confusion, you aren’t going to actually be able to retrieve the "resolution" of the users monitor, but you can access the size of their current window. You’ll just need to make a request and hit your WebMethod when the page loads via AJAX. Since
you already have jQuery included, it should be fairly trivial as seen below.

Within your Web Form, you would create a method that looks like the following :

public static void SetResolution(int height, int width)
       HttpContext.Current.Session["Resolution"] = new Size(width, height);

Notice the all important EnableSession attribute set to true. This will allow you to set the Session within a WebMethod. Next when your Page loads, you would make an AJAX call to set these values through jQuery using the following code :

<!-- Script to set resolution -->
<script type="text/javascript">
        $(function () {
            // When the page loads, store the resolution in the Session
                type: "POST",
                url: "Example.aspx/SetResolution",
                data: '{ width: ' + $('window').width() + ', height: ' + $('window').height() + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    // Do something to indicate your values were set

This is just an example of an approach of handling this, however could you elaborate on why you are storing the users resolution or window size within your server-side code? Basically all information related to the browser should be handled within the client.

You may want to consider taking a look at 51Degree’s "Foundation" project (also called the 51Degrees.Mobi project) , which greatly enhances the Mobile detection features available in .NET and is actually recommended by ASP.net as it provides better
detection for more recent devices.

You can read more about the Foundation project and download it here.

Hi Rion William ,

Thank you very much for your solution and suggestion

Leave a Reply