我如何使用asp.net获取浏览器的宽度

XuDong Peng-MSFT 10,181 信誉分 Microsoft 供应商
2024-03-12T03:14:33.8833333+00:00

你好,

我想获取web浏览器的宽度,但我尝试过但没有成功。

这是我的尝试:

Dim wid = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width

Dim hy = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

这段代码在服务器端不起作用,当我使用 visuel studio 时它工作正常

我尝试了使用 javascript 的另一个代码,并且宽度我无法在 vb.net 中调用它,就像变量为空没有值一样.

这是代码:

<script type="text/javascript">
function SetName() {
    var label = document.getElementById("<%=Label24.ClientID %>");
 
    //Set the value of Label.
    label.innerHTML = screen.width;
 
    //Set the value of Label in Hidden Field.
    document.getElementById("<%=Label26.ClientID %>").value = label.innerHTML;
}
</script>

这是我使用它的时候:

Protected Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        Label24.Text = (Request.Form(Label26.UniqueID))
End Sub

label24.text 没有值,我不知道为什么值没有传递。

请帮帮我。

Note: 该案例整理于:how i get the width of browser using asp.net

ASP.NET
ASP.NET
.NET Framework 中一套用于生成 Web 应用程序和 XML Web 服务的技术。
17 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Zhi Lv - MSFT 32,036 信誉分 Microsoft 供应商
    2024-03-12T05:35:21.5866667+00:00

    你好,

    您可以尝试以下方法:

    <input type="hidden" value="" name="clientScreenWidth" id="clientScreenWidth" /> 
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     
    <script src="Scripts/jquery-3.4.1.min.js"></script>
    <script>
      $(document).ready(function () {
        $("#clientScreenWidth").val($(window).width());      
      });
    </script>
     
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
          Dim width As String = HttpContext.Current.Request.Params("clientScreenWidth")
          If width IsNot Nothing Then
          End If
    End Sub
    

    结果如下:

    User's image


    如果答案是正确的解决方案,请单击“接受答案”并请投赞成票。如果您对此答案有其他疑问,请点击“评论”。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    最好的问候

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助