Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Detecting a Client's screen resolution in ASP.NET
 »  Home  »  Web Development  »  ASP.NET  »  Detecting a Client's screen resolution in ASP.NET
Detecting a Client's screen resolution in ASP.NET
by Serge Baranovsky | Published  12/08/2002 | Web Development ASP.NET | Rating:
Serge Baranovsky

Serge Baranovsky worked as a Director of Software Development for LYNX Medical Systems (lynxmed.com), based in Seattle, WA. He has previously worked as a consultant specializing in client-server and n-tier applications.

In his 12 year strong career as a professional developer, Serge has interacted with hundreds of developers and has developed various applications - from developer tools to mission critical banking and emergency medicine systems. He has a Masters degree in Rocket Science from Kharkov Aviation University. Originally from the Ukraine, Serge moved to the United States in January 1999.

Serge has been working with Visual Basic since the version 3.0 was introduced, and is a firm supporter of Microsoft's technology since then. Besides DevCity.NET, Serge also owns and maintains vbCity.com, a Visual Basic developer community website that provides help to the developers; and distributes developer tools for use with VB and .NET, including the widely popular PrettyCode.Print and CodeIt.Once.

 

View all articles by Serge Baranovsky...
Detecting a Client's screen resolution in ASP.NET

Article source code: screenresolution.zip

While there is no "native" to determine client bowser screen resolution in .NET Framework your ASP.NET application still can retrieve this information using little client-side JavaScript trick.

Into your page (default.aspx in our case) we want to add code that will redirect to the screen detection page if it has never been done yet in the current session - Session["ScreenResolution"] is null

default.aspx:

<script runat="server" language="C#">

    public void Page_Load(Object sender, EventArgs e){

        if (Session["ScreenResolution"] == null) {
            // Session variable is not set
            // Redirect to the screen resolution detection script
            Response.Redirect("detectscreen.aspx");
        } else {
            // Session variable is set
            // Display it on the page
            screenresolution.Text = Session["ScreenResolution"].ToString();
        }
        
    }

</script>

<HTML>
<BODY>
User screen resolution: <asp:label id="screenresolution" runat="server">undetected</asp:label>
</BODY>
<HTML>

The screen resolution detection script (detectscreen.aspx) determines the client screen height, width, color depth and redirects back to the default.aspx page

detectscreen.aspx:
<script runat="server" language="C#">

    public void Page_Load(Object sender, EventArgs e){

        if (Request.QueryString["action"] != null) {
            // store the screen resolution in Session["ScreenResolution"]
            // and redirect back to default.aspx
            Session["ScreenResolution"] = Request.QueryString["res"].ToString();
            Response.Redirect("default.aspx");
        }

    }
    // JavaScript code below will determine the user screen resolution
    // and redirect to itself with action=set QueryString parameter

</script>

<HTML><BODY>
<script language="javascript">
res = "&res="+screen.width+"x"+screen.height+"&d="+screen.colorDepth
top.location.href="detectscreen.aspx?action=set"+res
</script>

</BODY></HTML>

Here is yet another script that will be useful while testing the two above - it resets the Session["ScreenResolution"] variable to null

reset.aspx:
<script runat="server" language="C#">

    public void Page_Load(Object sender, EventArgs e){

        Session["ScreenResolution"] = null;

    }

</script>

Session["ScreenResolution"] has been reset to <i>null</i>

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.66169154228856 out of 5
 201 people have rated this page
Article Score110054
Sponsored Links