Link to home
Start Free TrialLog in
Avatar of CitySec
CitySec

asked on

Why put a login form in the master page when the content page renders first in the page lifecycle?

I have a simple application where the user can login and see previously submitted applications. I have the login form in my master page, and the form that displays previous applications in my content page. When the user logs in, logically the master page should authenticate them and then the content page, which uses a session variable created during authentication, should populate the form. However I was reading this page: http://msdn.microsoft.com/en-us/library/dct97kc3.aspx which I believe states that the content page does its VB processing before the master page does. That actually makes sense because in my application my form doesn't populate immediately after I login - rather I have to refresh the page first and then it populates. During debugging, my suspicions were confirmed by the fact that the content page VB processes before the master page.

My question is, is there a way to tell the master page VB to go before the content page VB?
Avatar of guru_sami
guru_sami
Flag of United States of America image

Don't think so but what you mean by "populate form"? Are you using any databound controls?
I think you might want to rebind those controls after successful login.
Avatar of Kelvin McDaniel
No, as far as I'm aware there's no way to re-order the page lifecycle.

I'd suggest you use the PreRender page event to handle the processing you're looking for or go with guru_sami's suggestion.
Avatar of CitySec
CitySec

ASKER

I'm manually binding form controls to a database. I actually am rebinding the controls after a successful login. The problem is, I have to do a double page refresh to get the form to bind. This is because of the sequence of evens happening during my page life cycle, which is:

1) Content page load (form data binding based on session variable)
2) Master page load (session variable created here)
3) Content control load
4) Master page control load

As you can see, 1) and 2) need to be swapped. Is there another event I can use besides page load?
Ok how are you setting forms authentication cookie?
May be we want to see some code...
Avatar of CitySec

ASKER

I'm not, I'm actually using a session variable instead.
Can we see some code?
Avatar of CitySec

ASKER

Here is the master page. User clicks button to login:

    Private Sub btnLoginSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLoginSubmit.Click
        Dim u As String = txtUsername.Text
        Dim p As String = txtPassword.Text

        authenticate("in", u, p, "", "")

    End Sub

    Public Sub authenticate(ByVal action As String, ByVal u As String, ByVal p As String, ByVal msg As String, ByVal err As String)
        lblGeneralError.Text = ""
        If action = "out" Then 'when the user logs out
            pnlLoggedOut.Visible = True
            pnlLoggedIn.Visible = False
            lblUserInfo.Text = ""
            lblLogin.Text = "Login"
            Session("userID") = 0
            Session("username") = ""
            Session("password") = ""
            lblGeneralError.Text = err
        ElseIf action = "in" Then 'when the user logs in
            Dim ds As DataSet = clsData.sqlLogin(u, p)
            If ds.Tables(0).Rows.Count > 0 Then
                Dim userID As Integer = ds.Tables(0).Rows(0)(0)
                Dim userEmail As String = ds.Tables(0).Rows(0)(3)
                Dim userCreated As String = CStr(ds.Tables(0).Rows(0)(4))
                Session("userID") = userID
                Session("username") = u
                Session("password") = p
                lblUserInfo.Text = "You are currently logged in as " & userEmail & "."
                lblLogin.Text = "Logout"
                pnlLoggedIn.Visible = True
                pnlLoggedOut.Visible = False
            Else
                authenticate("out", "", "", "", "We could not find your account. Please try again »")
            End If
        ElseIf action = "none" Then 'any time the user is not logging in or out
            If u = "" Or p = "" Then 'user is not logged in
                authenticate("out", "", "", "", "")
            Else 'user is logged in
                'do nothing
            End If
        End If

Here is the code to populate the form fields from the content page. You will see it requires a session variable of "userID" in order to populate.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        requestApp()
    End Sub

    Public Sub requestApp()
        'when a user logs in, we need to find a previously started app id
        'if there are no apps for this user, nothing will happen
        Dim appID As Integer = Session("appID")
        Dim userID As Integer = Session("userID")

        If appID <> 0 And userID <> 0 Then 'one previously started app found
            populateApp(appID)
        ElseIf userID <> 0 Then 'more than one previously started app found
            getSavedApps(userID)
        Else 'no previously started apps found
            'do nothing
        End If
    End Sub

I want the authenticate function on the master page to run before the requestApp function on the content page.
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial