Wednesday, August 2, 2006

Detecting Mobile Browsers

As discussed in our last meeting, web applications can be built to handle both PC browsers and mobile browsers. To do so, the application needs to detect if the browser is a mobile device and redirect the application to a page designed to display UI adequate for the smaller mobile browser. Detecting the browser is done using the following code sample, and it's typically placed in the Page_Load() of the login.aspx or default.aspx


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
If Not IsPostBack Then
If Request.Browser.IsMobileDevice = True Then
Response.Redirect("mobile/login.aspx")
End If
End If

Catch ex As Exception
ProcessEx(ex)
End Try

End Sub

5 comments:

  1. I want to know what your ProcessEx() does.

    ReplyDelete
  2. ProcessEx() is used as a central location for logging all Exceptions. In this particular instance, I simply write the error to a log file and then display the error to the user using a specific .ASPX page. Listed below is the code used for this scenario.

    Public Shared Sub ProcessEx(ByVal Ex As Exception)

    Dim strMsg As String
    strMsg = " Error=" & Ex.Message & ControlChars.CrLf
    strMsg &= " Method=" & System.Reflection.MethodInfo.GetCurrentMethod.Name & ControlChars.CrLf
    strMsg &= " Stack Trace=" & Ex.StackTrace & ControlChars.CrLf
    WriteEntry(strMsg)

    Dim cls As New clsOpMgmt
    cls.ShowError(strMsg)

    End Sub

    ReplyDelete
  3. Thanks. Now what does clsOpMgmt do?

    ReplyDelete
  4. clsOpMgmt is a class composed of functions used for handling errors. Some of the methods include WriteEntry() which writes to a log file, SendEmail() used for sending automated emails when certain erros occur, and ShowError() which is used to display an error or exception in a user friendly manner.

    ReplyDelete
  5. You could also use a service like handsetdetection.com

    ReplyDelete