How to Determine Browser Window Size on Server Side C#

How can I determine browser window size on server side C#

You can use Javascript to get the viewport width and height. Then pass the values back via a hidden form input or ajax.

At its simplest
var width = $(window).width();
var height = $(window).height();
Complete method using hidden form inputs

Assuming you have: JQuery framework.

First, add these hidden form inputs to store the width and height until postback.

<asp:HiddenField ID="width" runat="server" />
<asp:HiddenField ID="height" runat="server" />

Next we want to get the window (viewport) width and height. JQuery has two methods for this, aptly named width() and height().

Add the following code to your .aspx file within the head element.

<script type="text/javascript">
$(document).ready(function() {

$("#width").val() = $(window).width();
$("#height").val() = $(window).height();

});
</script>

Result

This will result in the width and height of the browser window being available on postback. Just access the hidden form inputs like this:

var TheBrowserWidth = width.Value;
var TheBrowserHeight = height.Value;

This method provides the height and width upon postback, but not on the intial page load.

Note on UpdatePanels: If you are posting back via UpdatePanels, I believe the hidden inputs need to be within the UpdatePanel.

Alternatively you can post back the values via an ajax call. This is useful if you want to react to window resizing.

Update for jquery 3.1.1

I had to change the JavaScript to:

$("#width").val($(window).width());
$("#height").val($(window).height());

Asp.Net Get Screen Width

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.

ASP.NET Get window size in page load

You won't get page or windows size at server side i.e on page load function, but you will get it using client side script i.e jquery or javascript

<script type="text/javascript">
$(document).ready(function()
{
var w = $(window).width();
var h = $(window).height();
});
</script>

And later on you can do your functionality when resize the window by using following function

window.onresize = function(event) 
{
var height=$(window).height();
var width=$(window).width();
//your functionality
}

ASP.NET c# get screen width in pixels

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

Is there a way to find the browser window height and width in VB.Net without having using javascript?

Quick answer: No, you cannot.

BUT: You may have access to those values by looking at the request headers values.

Please note that the value may not always be there and that different browser may or may not sent those values with different keys.

The best way to have this value should be usign javascript or vbscript (ie CLIENT script). You may use ajax you create your image async way.

ASP.NET is a SERVER side programming language (like JSP or PHP) and has nothing to do with which browser access it...

Look at it this way, what is the screen size of Google Bot "browser" ? Or what if a access your site with telnet ?

So you should use client script to have acces to client properties.

Do not hesitate to comment if I am not clear or right.

C# -> Detetcting Browser Width

I would make a hiddenfield and fill it by using jQuery http://api.jquery.com/width/

$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

So you can choose window or document and fill a hiddenfield

<asp:HiddenField ID="hf" runat="Server" Value="" ClientIDMode="static" />

Please note that clientidmode is only for asp.net 4 and above
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

$("#<%= hf.ClientID %>").val($(window).width()); //asp.net 3.5 or lower
$("#hf").val($(window).width()); //asp.net 4 or higher

edit another option is to use jquery and do an ajax post to an *.ashx or *.asmx

How to get the document height using C#?

You can't get this value using server-side code, as it is, by definition, server-side.

You could however use JavaScript to pass the value back to an ASP.NET script via AJAX, and take whatever action you need to take at this point. Of course this means that you wouldn't be able to, for example, provide different content onload for different screen sizes, but it all depends on your intentions.

Since you need it on pageload, one very very hacky way to do this would be to detect the browser height and then redirect, for example pass it as a querystring value that you can use directly within PageLoad. What I mean specifically is, use JavaScript to detect this QS value (for example ?ph=768), and if it's not there, redirect the page to itself, appending on ?ph=768. That way you can use it to get the page height on document load.

As mentioned by @J.Steen in comments, though, it's probably better to define your intentions.



Related Topics



Leave a reply



Submit