asp.netvb.netmaster-pagespreinit

How to set master page on page preinit based on custom user control method result


I have a user control that checks if a certain query string and session value are present then returns a boolean based on that, if it's true I want to set the master page.

The page is throwing an Object reference exception when it tries to call the method EditUser1.UserAuthorization(). Why is this happening? I imagine that the method doesn't exist at that point in the stack.

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
    If EditUser1.UserAuthorization(True) Then
        Page.MasterPageFile = "APERSEmpCont.master"
    End If
End Sub

Solution

  • I just found out from here, that the page controls haven't been initialized at the point of the preinit, so that method doesn't exist at that moment. I'll have to move the method to the page level to make it work.