locked
Request.Browser.IsMobileDevice Does not work in .Net 2.0 RRS feed

  • Question

  • User-939035612 posted

    I have an old .net 2.0 site that I need to make mobile friendly but I can't set the mobile masterpage the way i do in .net 4. The code I have is as follows:

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            If Request.Browser.IsMobileDevice Then
                Me.MasterPageFile = "~/Site.Mobile.Master"
            End If
        End Sub

    Sunday, September 22, 2019 4:17 AM

Answers

  • User-939035612 posted

    Fixed that by removing Master virtualpath from the page'

    And adding 

    Public ReadOnly Property ScriptManager1() As ScriptManager
            Get
                Dim ScriptManagerRef As ScriptManager = CType(Page.Master.FindControl("ScriptManager1"), ScriptManager)
                Return ScriptManagerRef
            End Get
        End Property
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim ScriptManagerRef As ScriptManager = CType(Page.Master.FindControl("ScriptManager1"), ScriptManager)
            AddHandler ScriptManagerRef.Navigate, AddressOf ScriptManager1_Navigate
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 22, 2019 11:20 PM

All replies

  • User-939035612 posted

    I found a somewhat fix. I created a class called setmasterpage.vb with the following function

     Public Shared Function GetMaster(ByVal agent As Object) As String
            Dim UserAgent As String = agent.ToString
            Dim strMP As String = String.Empty
            If UserAgent.Contains("Phone") OrElse UserAgent.Contains("Android") OrElse UserAgent.Contains("Nexus") OrElse UserAgent.Contains("Mobile") Then
                strMP = "~/Site.Mobile.Master"
            Else
                strMP = "~/FinalMaster.Master"
            End If
            Return strMP
        End Function

    Then on each Default.aspx.vb page I have the following:

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            Dim useragent = Request.ServerVariables("HTTP_USER_AGENT")
            Me.MasterPageFile = setmasterpage.GetMaster(useragent)
        End Sub

    That works great on pages that do not have update panels. If I have an update panel I get a runtime error that says:

    Server Error in '/' Application.


    Unable to cast object of type 'ASP.site_mobile_master' to type 'ASP.finalmaster_master'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.site_mobile_master' to type 'ASP.finalmaster_master'.

    Source Error:

    Line 397:
    Line 398:    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Line 399:        AddHandler Master.ScriptManagerRef.Navigate, AddressOf ScriptManager1_Navigate
    Line 400:    End Sub
    Line 401:    Protected Sub ScriptManager1_Navigate(ByVal sender As Object, ByVal e As System.Web.UI.HistoryEventArgs)


    Source File: C:\Users\me\Documents\Visual Studio 2010\Websites\mysite\videos\library.aspx.vb    Line: 399

    Stack Trace:

    [InvalidCastException: Unable to cast object of type 'ASP.site_mobile_master' to type 'ASP.finalmaster_master'.]
       videos_library.get_Master() +74
       videos_library.Page_Init(Object sender, EventArgs e) in C:\Users\me\Documents\Visual Studio 2010\Websites\mysite\videos\library.aspx.vb:399
       System.Web.UI.Control.OnInit(EventArgs e) +99
       System.Web.UI.Page.OnInit(EventArgs e) +12
       System.Web.UI.Control.InitRecursive(Control namingContainer) +333
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378

    Sunday, September 22, 2019 10:04 PM
  • User-939035612 posted

    Fixed that by removing Master virtualpath from the page'

    And adding 

    Public ReadOnly Property ScriptManager1() As ScriptManager
            Get
                Dim ScriptManagerRef As ScriptManager = CType(Page.Master.FindControl("ScriptManager1"), ScriptManager)
                Return ScriptManagerRef
            End Get
        End Property
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim ScriptManagerRef As ScriptManager = CType(Page.Master.FindControl("ScriptManager1"), ScriptManager)
            AddHandler ScriptManagerRef.Navigate, AddressOf ScriptManager1_Navigate
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 22, 2019 11:20 PM