How to Track Browsing Device and Orientation in ASP.Net

In this article, you will learn how to track the browsing device including its orientation, whether it's an iPad, iPod, iPhone or Mobile in ASP.Net.

First of all add two meta tags inside the head tag:

  1. <meta name="HandheldFriendly" content="true" />  
  2. <meta name="viewport" content="width=device-width, maximum-scale=1.0" />   

Next let's check whether we are browsing from a device or a Desktop.

  1. protected void btnTrackDevice_Click(object sender, EventArgs e)  
  2. {  
  3.     HttpRequest httpRequest = HttpContext.Current.Request;  
  4.     if (httpRequest.Browser.IsMobileDevice)  
  5.     {  
  6.         string redirectTo = "Mobile/Default.aspx";  
  7.         HttpContext.Current.Response.Redirect(redirectTo);  
  8.     }  
  9.     else  
  10.     {  
  11.         HttpContext.Current.Response.Redirect("Desktop/Default.aspx");  
  12.     }  
  13. }  

Let's add a few device names in am array list.

  1. protected void btnTrackDevice1_Click(object sender, EventArgs e)  
  2. {  
  3.     //This code is used to get device name from array and redirect to page accoring to device  
  4.     bool IsDevice = false;  
  5.     string[] Devices = new string[] { "iPhone""iPad","iPod","BlackBerry",  
  6.                                                  "Nokia""Android""WindowsPhone",  
  7.                                                  "Mobile"  
  8.                                                  };  
  9.     foreach (string MobileDeviceName in Devices)  
  10.     {  
  11.         if ((Request.UserAgent.IndexOf(MobileDeviceName, StringComparison.OrdinalIgnoreCase)) > 0)  
  12.         {  
  13.             IsDevice = true;  
  14.             break;  
  15.         }  
  16.     }  
  17.     if (IsDevice)  
  18.     {  
  19.         Response.Redirect("Mobile/Default.aspx");  
  20.     }  
  21.     else  
  22.     {  
  23.         Response.Redirect("Desktop/Default.aspx");  
  24.     }  
  25. }

Or if you want to, you can keep device names in a XML file so later on you can add or remove any device from the list more easily.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Devices>  
  3.   <Device>iPad</Device>  
  4.   <Device>iPhone</Device>  
  5.   <Device>iPod</Device>  
  6.   <Device>WindowsPhone</Device>  
  7.   <Device>Android</Device>  
  8.   <Device>BlackBerry</Device>  
  9.   <Device>Nokia</Device>  
  10.   <Device>Samsung</Device>  
  11.   <Device>Mobile</Device>  
  12. </Devices>   

  1. protected void btnTrackDevice2_Click(object sender, EventArgs e)  
  2. {  
  3.     //This code used to get devices from xml file  
  4.     bool IsDevice = false;  
  5.     XDocument DeviceList = XDocument.Load(Server.MapPath("file/MobileDevices.xml"));  
  6.     var mobileDevices = from devices in DeviceList.Root.Elements()  
  7.                         select devices;  
  8.     foreach (XElement device in mobileDevices)  
  9.     {  
  10.         if (!string.IsNullOrEmpty(device.Value))  
  11.         {  
  12.             if ((Request.UserAgent.IndexOf(device.Value, StringComparison.OrdinalIgnoreCase)) > 0)  
  13.             {  
  14.                 IsDevice = true;  
  15.                 break;  
  16.             }  
  17.         }  
  18.     }  
  19.     if (IsDevice)  
  20.     {  
  21.         Response.Redirect("Mobile/Default.aspx");  
  22.     }  
  23.     else  
  24.     {  
  25.         Response.Redirect("Desktop/Default.aspx");  
  26.     }  
  27.     //end  
  28. }  

Now let's track the device orientation mode.

Safari on the iPad does support the window.orientation property, so if necessary, you can use that to determine if the user is in horizontal or vertical mode. As a reminder of this functionality:

  1. <button onclick="detectIPadOrientation();">  
  2.             What's my Orientation?</button>  
  3. <script type="text/javascript">  
  4.         alert(window.onorientationchange);  
  5.         window.onorientationchange = detectIPadOrientation;  
  6.         function detectIPadOrientation() {  
  7.             if (orientation == 0) {  
  8.                 alert('Portrait Mode, Home Button bottom');  
  9.             }  
  10.             else if (orientation == 90) {  
  11.                 alert('Landscape Mode, Home Button right');  
  12.             }  
  13.             else if (orientation == -90) {  
  14.                 alert('Landscape Mode, Home Button left');  
  15.             }  
  16.             else if (orientation == 180) {  
  17.                 alert('Portrait Mode, Home Button top');  
  18.             }  
  19.         }   
  20. </script>  

There is also the orientationchange event that fires on the window object when the device is rotated.

Or you can use CSS.

 

  1. <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">   
  2.   
  3. <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">   

Or

 

  1. $(window).bind('orientationchange'function(event) {  
  2.   alert('new orientation:' + event.orientation);  
  3. });


Similar Articles