11

I am trying to retrieve width of my browser in pixels but for some reason I am getting back 640, my resolution is at 1240.

Code I am using is Request.Browser.ScreenPixelsWidth

Does anyone knows why it returns 640 always or if there is another way for me to get the width of the browser upon page load?

5
  • 4
    So you want the client's browser width within the code you're executing on the server?
    – qJake
    Commented May 24, 2013 at 19:16
  • 1
    you can get those values in Javascript and pass them on to the server.. see this page responsejs.com/labs/dimensions
    – Amitd
    Commented May 24, 2013 at 19:20
  • Plus you're asking for the browser width but trying to detect the screen width - which is it you want as they are not always the same ;)
    – LDJ
    Commented May 24, 2013 at 19:55
  • 1
    @Amitd do you have an example of how to pass those js values to the server?
    – Cole
    Commented Feb 26, 2014 at 19:40
  • @Cole try this answer stackoverflow.com/questions/11628859/…
    – Amitd
    Commented Mar 1, 2014 at 14:38

2 Answers 2

11

You need to populate a hidden field with Javascript (see this question for how to do that), and then send that field back to the server so that it's avaialble within ASP.NET.

ASP.NET is incapable of reading detailed browser information like that directly (you're talking about sending some very specific information from the client's browser to the ASP.NET server where your application is hosted, and there are a lot of problems involved with doing something like that).

See this similar, but less detailed, question:

Asp.Net Get Screen Width

13
  • I don't quite understand how they are doing it, would you be able to give me a line of code that will allow me to get the browser width and put it in a c# variable for my use?
    – Bagzli
    Commented May 24, 2013 at 19:22
  • 1
    No, that's what I'm trying to tell you, it's not possible without setting up some Javascript on the page, a hidden ASP.NET input field, and a way to retrieve the value from that hidden field via C#. It's a lot more than one line.
    – qJake
    Commented May 24, 2013 at 19:24
  • can you then tell me how do I pass a value into the lets say a hidden label through jQuery? which has an ID of "test"
    – Bagzli
    Commented May 24, 2013 at 19:34
  • 1
    Within your document.ready function: $('#test').html($(window).width()); - will set the label's inner HTML to be the width of the browser window.
    – qJake
    Commented May 24, 2013 at 19:36
  • I tried that, however my c# is not detecting it. here is what I tried. - $('#myP').text($(window).width()); - then ASP.NET - <p id="myP" runat="server" name="myP"></p> - then c# - lblTestLabel.Text = myP.InnerText; the value displayed of the test label is nothing, the value displayed of the p is the pixel width - I also tried innerhtml, same thing.
    – Bagzli
    Commented May 24, 2013 at 19:52
8

try this

int x = (Request.Browser.ScreenPixelsWidth)*2-100;

you can set width according to your requirement

1
  • The documentation on ScreenPixelsWidth says "The approximate width of the display, in pixels.". It is based on "default font sizes", "device-specific sizes" and other data. So the value is very unreliable. But it is the asssumed width of the display, in pixels. Converting it so it works for this example does not make it generally correct. I tested for different screen sizes and ScreenPixelsWidth always returned 640. Your calculation would be wrong for all this cases. It is only correct if the screen width is 1240 so you could hardcode the value 1240 as well.
    – David
    Commented Mar 5, 2020 at 12:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.