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?

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • 4
    So you want the *client's* browser width within the code you're executing on the *server*? – qJake May 24 '13 at 19:16
  • 1
    you can get those values in Javascript and pass them on to the server.. see this page http://responsejs.com/labs/dimensions/ – Amitd May 24 '13 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 May 24 '13 at 19:55
  • 1
    @Amitd do you have an example of how to pass those js values to the server? – Cole Feb 26 '14 at 19:40
  • @Cole try this answer http://stackoverflow.com/questions/11628859/how-can-i-determine-browser-window-size-on-server-side-c-sharp – Amitd Mar 01 '14 at 14:38

2 Answers2

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

Community
  • 1
  • 1
qJake
  • 16,821
  • 17
  • 83
  • 135
  • 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 May 24 '13 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 May 24 '13 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 May 24 '13 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 May 24 '13 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 -

    - 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 May 24 '13 at 19:52
  • You need to make sure your label is an ASP-tracked object (it must be an `asp:label` with `runat="server"` and an ID, not just a `p`). You also need to make sure your code posts back to the server so that the server can see and reflect the change into your C# code. – qJake May 24 '13 at 19:56
  • I tried doing it that way first, but i cannot get jQuery to populate the value of the jsp label. Do you by any chance have an example of that? I tried what these guys suggested, but my page wont even load, it crashes. http://stackoverflow.com/questions/2493209/how-to-fill-a-label-text-property-via-jquery – Bagzli May 24 '13 at 19:59
  • Did you include the jQuery in your document.ready function? `$(function() { /* put the window width code here */ });` – qJake May 24 '13 at 20:02
  • - that is basically what i have in the jquery. If I myP is a paragraph then it works, if myP is a jsp label then it does not works. – Bagzli May 27 '13 at 12:04
  • so I finally got it to work. For those that are having similar problems to me here is the solution: $(document).ready(function () { $(function () { $('#myInput').val($(window).width()); }); }); The ASP.NET code is: and the c# code is: myLabel.Text = myInput.Value; SpikeX thanks for all the help! – Bagzli May 27 '13 at 12:43
  • Using jQuery will be the best idea. `$('#someelement').html($(window).width());` The html will write the data in the div or span with the id someelement. – Afzaal Ahmad Zeeshan Aug 19 '13 at 13:30
  • is there anyway that u can determine the window size on page_load? what i mean is when the Page_Load function in C# fires, u can use the value of myInput??? – CodeMonkey Sep 03 '13 at 03:28
  • No, you would need to do a postback first, or determine the width on another page and send it in (and hope they didn't resize the browser). – qJake Sep 07 '13 at 23:34
8

try this

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

you can set width according to your requirement

  • 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 Mar 05 '20 at 12:47