如何确定服务器端的C#浏览器窗口大小 [英] How can I determine browser window size on server side C#

查看:417
本文介绍了如何确定服务器端的C#浏览器窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到当前打开的浏览器屏幕窗口的确切高度和宽度?

How can I get the exact height and width of the currently open browser screen window?

推荐答案

您可以使用JavaScript来获取视野的宽度和高度。然后,通过一个隐藏的表单输入或AJAX的值传递回来。

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

var width = $(window).width();
var height = $(window).height();

完整方法,使用隐藏的表单输入

假设你已经: jQuery框架

首先,添加这些隐藏的表单输入存储的宽度和高度,直到回传。

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" />

接下来,我们想要得到的窗口(视口),宽度和高度。 jQuery有两种方法来此,取名为宽()和高度()。

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

添加以下code到您的.aspx文件头元素中。

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>

结果

这将导致在浏览器窗口上可用的回发的宽度和高度。刚刚访问隐藏的表单输入是这样的:

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;

注意上的UpdatePanel:如果您通过的UpdatePanel回发,我相信隐藏的输入必须在UpdatePanel在

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

另外,您可以通过Ajax调用回发的值。如果要应对调整窗口大小,这非常有用。

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

这篇关于如何确定服务器端的C#浏览器窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆