且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定服务器端的C#浏览器窗口大小

更新时间:2023-11-27 22:26:04

您可以使用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.