how i get the width of browser using asp.net

HOUSSEM MAHJOUBI 286 Reputation points
2023-03-09T09:48:19.1066667+00:00

Hi members

i want get the width of the web browser and i tried without succeed.

this is my try :

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

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

this code doesn't work in server side it work fine when i use visuel studio

i tried another code using javascript and the width i can't call it in vb.net like the variable is empty has no value

this is the code :

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

and this is when i use that :

 Protected Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click

        Label24.Text = (Request.Form(Label26.UniqueID))


    End Sub

the label24.text has no value i have no idea why the value didn't pass.

please help

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,263 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
871 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,574 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,636 Reputation points Microsoft Vendor
    2023-03-10T05:59:58.1266667+00:00

    Hi @HOUSSEM MAHJOUBI,

    You can try the following methods:

    <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

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Viorel 112.4K Reputation points
    2023-03-09T11:09:53.68+00:00

    Show more details about Label26. I think that you should use something like this:

    <asp:HiddenField id="Label26" runat="server"/>
    

    Then try this: Dim width As String = Label26.Value. Check if width contains the value obtained by JavaScript.


  2. Bruce (SqlWork.com) 56,531 Reputation points
    2023-03-09T16:45:53.95+00:00

    first screen.width is the monitor size, not the browser. you probably want window.innerWidth. second as the user can resize the browser, its of little use to the server. you should use responsive design.