How can I get the screen width on the server side in an Asp.net (C#) project?
Asked
Active
Viewed 5.7k times
4 Answers
10
Place this on your form:
<input type="hidden" value=""
name="clientScreenHeight" id="clientScreenHeight" />
<input type="hidden" value=""
name="clientScreenWidth" id="clientScreenWidth" />
This is onload script:
$(document).ready(function () {
$("#clientScreenWidth").val($(window).width());
$("#clientScreenHeight").val($(window).height());
});
This is server side code:
string height = HttpContext.Current.Request.Params["clientScreenHeight"];
string width = HttpContext.Current.Request.Params["clientScreenWidth"];

Andrew Barber
- 39,603
- 20
- 94
- 123

flap13
- 379
- 3
- 14
-
Just a note to say be careful of whether you want window or screen width. In Javascript screen means the display so use screen.width. Use the stated code for the window width i.e. you will get the resized window size if the user is not running full screen etc. This is potentially critically important depending on what you are doing with the numbers. – Alan Macdonald Aug 24 '15 at 14:54
-
Tried adding the hidden input values clientScreenHeight and clientScreenWidth in the aspx file in a VS C# project and adding the onload script to the Page_Load function called when the page loads using ScriptManager.RegisterStartupScript(this, GetType(), "ClientViewport", script, true) with script as shown above. When I use Server Side code to retrieve height & width they contain only empty string set in the aspx file. The script to store values is not working, but it does not seem to have any errors. Am I putting the input hidden objects in the right place and executing the script correctly? – SimonKravis Feb 01 '18 at 01:59
-
The problem was that the ScriptManager control in the Master page of teh form I was loading did not have any scripts in it. Adding the following scripts resulted in the Server Side code retrieving correct values: – SimonKravis Feb 01 '18 at 08:26
-
9
You could read it with javascript and submit the results to the server.
A server-side-only solution can not exist, since html does not submit such data automatically in requests.

TGlatzer
- 5,815
- 2
- 25
- 46
-
@Readers Would recommend reading some of the answer below as well as Yitzhak or50 gave the answer I was looking for. – Lsakurifaisu Apr 19 '16 at 15:54
-
@Readers so what happens when screen size needed on homepage when still no requests have been done? – Kalin Krastev Apr 27 '18 at 20:29
3
to get the Characters
Request.Browser.ScreenCharactersWidth
Request.Browser.ScreenCharactersHeight
to get the Resolution
you need to send the data from client side with javascript or jquery i use this code its good the work
var ScreenPixelsHeight = window["innerHeight"];
var ScreenPixelsWidth = window["innerWidth"];
var JSLink = "http://www + "&ScreenPixelsHeight="+ScreenPixelsHeight+
"&ScreenPixelsWidth="+ScreenPixelsWidth;

Yitzhak Weinberg
- 2,324
- 1
- 17
- 21
-
As MSDN (https://msdn.microsoft.com/en-us/library/aa710891.aspx) states: "These values are not necessarily exact" - and the default value is "96". – TGlatzer Apr 25 '16 at 10:33
3
Use code below
int width = (Request.Browser.ScreenPixelsWidth) * 2 - 100;
int height = (Request.Browser.ScreenPixelsHeight) * 2 - 100;

Saranjith
- 11,242
- 5
- 69
- 122

bilal chaudhari
- 79
- 4
-
2This code returns the same values irrespective of the actual size of the browser window. Emulating different browsers using UserAgent spoofing on Chrome UA Spoofer and changing window size does not change these values. – SimonKravis Feb 01 '18 at 01:39