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:4.03658536585367 out of 5
 82 people have rated this page
Article Score35736
Comments    Submit Comment

Comment #1  (Posted by Nimesh Gala on 12/19/2002)

what if the client changes the screen resolution in between?
 
Comment #2  (Posted by Jack Raisor on 02/19/2003)

Well, Serge

... that was a pretty nice idea ... I only wonder why the javascript keeps popping ... ;-)

Best regards, Raisor
 
Comment #3  (Posted by Duncan C. Ion on 02/24/2003)

.. a nice little utility. Thanks.
I guess the best use of this utilty would be to alter the font size of text relevent to the session value. That way, you can alter the webpage presentation accordingly. I was thinking that I might dynamically change the css file based on this value.

regards,
Duncan.
 
Comment #4  (Posted by Verena Guardarrama on 09/24/2003)

Ok, I detecting a clients resolution, I need code in asp for change this resolution. Example I need that all web user from my application have the screen resolution to 1024 by 764 pixels how change this resolution in the client pc. Thank.
 
Comment #5  (Posted by Ken Banks on 09/13/2004)

Were very helpful. Only took very slight modification to make work in VB.net
 
Comment #6  (Posted by an unknown user on 04/04/2005)
Rating
How many round trips?
 
Comment #7  (Posted by an unknown user on 04/09/2005)
Rating
well still i need some clearer
 
Comment #8  (Posted by an unknown user on 04/10/2005)
Rating
This article has just told me exactly what I needed to know and suggested a neat implementation.
 
Comment #9  (Posted by Thiago RS on 04/12/2005)
Rating
A more elegant way to:
Session["ScreenResolution"] = null;

is:
Session.Remove("ScreenResolution");
 
Comment #10  (Posted by an unknown user on 04/27/2005)
Rating
The problem with this solution is that it removes any querystring values that might be passed into the initial document.

Regards,

Graham
graham_oriley@hotmail.com
 
Comment #11  (Posted by Graham O'Riley on 04/27/2005)
Rating
The problem with this solution is that it removes any querystring values that might be passed into the initial document. Regards, Graham
 
Comment #12  (Posted by an unknown user on 07/15/2005)
Rating
I was actually researching browser detection methods, but found this little trick quite useful. I will most definitely use it --- Thanks.
 
Comment #13  (Posted by an unknown user on 07/27/2005)
Rating
How do I prevent detectscreen.aspx to occure in browser history. As it works now it's impossible to move back in browser history.
 
Comment #14  (Posted by an unknown user on 08/23/2005)
Rating
Very good I always want to do this, i didn't know how, thanks.

Rui Sabino
 
Comment #15  (Posted by an unknown user on 09/22/2005)
Rating
It works
 
Comment #16  (Posted by Philip on 10/11/2005)
Rating
Thanks. I needed this badly.
As for some of these comments
If default.aspx is the first page of your web application I don't see why there's need for a query string to be preserved. And who wants to be so stupid to force an other screen resolution upon a user. This worked out fine for me with some addaptions. I only need the availiable height to build some div's. So using other client figures than this you could build an arry to post back.
 
Comment #17  (Posted by an unknown user on 12/10/2005)
Rating
public int rate(string code)
{
if code == success
return 5;
else
reurn 0;


}
 
Comment #18  (Posted by an unknown user on 01/24/2006)
Rating
I Love It
 
Comment #19  (Posted by Manas Das on 02/21/2006)
Rating
This is very good, I have used it in my project. Thanks.
 
Comment #20  (Posted by Arabinda Sarkar on 02/21/2006)
Rating
This is good.
 
Comment #21  (Posted by an unknown user on 02/28/2006)
Rating
helpful stuff
 
Comment #22  (Posted by an unknown user on 03/22/2006)
Rating
Thanks
 
Comment #23  (Posted by an unknown user on 04/19/2006)
Rating
Top banana! Does exactly what is says on the packet. Thanks.
 
Comment #24  (Posted by an unknown user on 08/12/2006)
Rating
it good example not only good example but good explain about problem.. thank you..
 
Comment #25  (Posted by an unknown user on 09/08/2006)
Rating
Exactly what i was seeking, thank you!
 
Comment #26  (Posted by an unknown user on 10/01/2006)
Rating
If client browser does not support Javascript, then the target page is not going to be reachable.
 
Comment #27  (Posted by an unknown user on 10/20/2006)
Rating
This adaptation of your idea works nicely.

If Not String.IsNullOrEmpty(Page.Request("res")) Then
Session("res") = Page.Request("res").ToString
End If

If IsNothing(Session("res")) Then
Response.Write("")
Exit Sub
End If

Stuart Rickard
stuart (stop) rickard (at-sign) googlemail (stop) com
 
Comment #28  (Posted by an unknown user on 01/20/2007)
Rating
this is so simple way to do this. There's no new interesting thing on this article for me
 
Comment #29  (Posted by an unknown user on 02/09/2007)
Rating
it is best solutions for the beginers.it explains each & every consept when we read.
i got the solution by reading this code.
thank you lot.
 
Comment #30  (Posted by an unknown user on 02/09/2007)
Rating
man i am also hav a Bachelor and a Master degree in computer engennering and netowrk from Radio-Electronic Engineering from Kharkov i work for Denali advanced integration as developer, very good article i gonna use it in hdnw.com, thank you
 
Comment #31  (Posted by an unknown user on 02/12/2007)
Rating
These scripts will unfortunately play havoc to crawlers and people navigating in from search engine results.

It can possibly be adapted using 'request.Browser.Crawler', however I am unsure how accurate this command is.
 
Comment #32  (Posted by an unknown user on 03/14/2007)
Rating
I was looking for a VB solution. I see the need for javascript, but can't the C# code be written in VB? Here is my solution: default.aspx.vb contains this code - If Request.QueryString("ScreenWidth") <> "" Then
Session("ScreenWidth") = Request.QueryString("ScreenWidth")
Else
If Session("ScreenWidth") = "" Then
Response.Redirect("detect.aspx", True)
End If
End If
and detect.aspx contains this code -

 
Comment #33  (Posted by an unknown user on 03/16/2007)
Rating
You solved my problem very cleaver idea, Thanks for your help :-)
 
Comment #34  (Posted by an unknown user on 03/16/2007)
Rating
You solved my problem very cleaver idea, Thanks for your help :-)
 
Comment #35  (Posted by an unknown user on 04/28/2007)
Rating
Simple & Clean - Thanks
 
Comment #36  (Posted by an unknown user on 05/03/2007)
Rating
It works, thank
 
Comment #37  (Posted by an unknown user on 05/29/2007)
Rating
i want to change screen resolution to the cliend system in asp.net. its possible or not.
 
Comment #38  (Posted by an unknown user on 06/08/2007)
Rating
Excellent!
Thank you!
 
Comment #39  (Posted by Serge Baranovsky on 07/04/2007)
Rating
2unknown user: No, it is not possible to change screen resolution from ASP.NET. Nor it is possible in any other browser based environment.
 
Comment #40  (Posted by an unknown user on 08/10/2007)
Rating
Excellent article. But, default.aspx is not ALWAYS the first page that users go to on my site. The detectscreen.aspx is set up so that 'action' is not null, so it can't receive a request querystring containing a page name to re-direct back to. It's hard coded to always redirect to default.aspx. If my site allows users to enter urls to many other pages into their browsers (bypassing default.aspx), this solution won't help me keep ALL my pages aware of the current client screen resolution. There must be a more global solution, so that all pages 'know' what resolution they are dealing with. Thanks...
 
Comment #41  (Posted by an unknown user on 08/30/2007)
Rating
estimado:
funciona espectacular el codigo ;0)

saludos!
 
Comment #42  (Posted by an unknown user on 11/26/2007)
Rating
Set the screen information in Application collection , this can be used through out the app - all pages - unless session expires or you forcefully set it to null :)
 
Comment #43  (Posted by an unknown user on 04/25/2008)
Rating
I was searching for the thing for many days. nice article.
Thanx
Anagha
 
Comment #44  (Posted by an unknown user on 05/28/2008)
Rating
give me the solution of screen resolution in asp.net

 
Comment #45  (Posted by an unknown user on 07/03/2008)
Rating
I one file no redirect, no session works with IE and Firefox:







Best Regards
Luciano, www.D4Internet.com

 
Comment #46  (Posted by an unknown user on 07/14/2008)
Rating
not impressed
 
Sponsored Links