ASP.NET c# 以像素为单位获取屏幕宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16742102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:48:47  来源:igfitidea点击:

ASP.NET c# get screen width in pixels

c#asp.net

提问by Bojan

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.

我试图以像素为单位检索浏览器的宽度,但由于某种原因,我返回 640,我的分辨率为 1240。

Code I am using is Request.Browser.ScreenPixelsWidth

我正在使用的代码是 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?

有谁知道为什么它总是返回 640 或者是否有另一种方法可以让我在页面加载时获得浏览器的宽度?

采纳答案by qJake

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

您需要使用 Javascript 填充一个隐藏字段(有关如何执行操作,请参阅此问题),然后将该字段发送回服务器,以便它在 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).

ASP.NET 无法直接读取这样的详细浏览器信息(您指的是将一些非常具体的信息从客户端的浏览器发送到托管您的应用程序的 ASP.NET 服务器,并且这样做涉及很多问题类似的东西)。

See this similar, but less detailed, question:

看到这个类似但不太详细的问题:

Asp.Net Get Screen Width

Asp.Net 获取屏幕宽度

回答by qJake

try this

尝试这个

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

you can set width according to your requirement

您可以根据您的要求设置宽度