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

Microsoft 365 and Office Development Office JavaScript API
Developer technologies VB
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    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 122.5K 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) 77,686 Reputation points Volunteer Moderator
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.