Insights on Software Development and Architecture

ErionPC's weblog on software development

ASP.NET – Get Client Screen Resolution

No matter ASP.NET offers the “Page.Request.Browser.ScreenPixelsWidth” and “Page.Request.Browser.ScreenPixelsHeight” methods, these don’t help when you need to obtain the client’s screen resolution on the server side. They give false results. I think the best way to do this is through JavaScript. Here’s an example:

C#


<%if (!this.IsPostBack)
{%>
<script language="javascript" type="text/javascript">
var rowURL = window.location.href;
var screenWidth = window.screen.availWidth;
if (rowURL.indexOf("screen_width") == -1)
window.location.href = window.location.href + "?screen_width=" + screenWidth;
</script>
<%}%>

This script just needs to be pasted inside the <body> tag of the page. The script basically checks whether the query string contains the “screen_width” parameter. If it doesn’t, it appends the “screen_width” parameter to the query string and refreshes the page. Server-side, you just need to retreive the value of the “screen_width” Request parameter.

Advertisements

2 responses to “ASP.NET – Get Client Screen Resolution

  1. Ravi Tilala 31 August 2012 at 7:0

    Thank you

  2. Joe 21 May 2014 at 16:0

    You have given me a very simple solution to my problem. Whereas everyone else wanted me to write the c# equivalent of War & Peace!
    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: