Insights on Software Development and Architecture

ErionPC's weblog on software development

Monthly Archives: September 2009

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.