Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

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?
0
CitySec
Asked:
CitySec
  • 4
  • 3
1 Solution
 
guru_samiCommented:
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.
0
 
Kelvin McDanielSr. Developer/ConsultantCommented:
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.
0
 
CitySecAuthor Commented:
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?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
guru_samiCommented:
Ok how are you setting forms authentication cookie?
May be we want to see some code...
0
 
CitySecAuthor Commented:
I'm not, I'm actually using a session variable instead.
0
 
guru_samiCommented:
Can we see some code?
0
 
CitySecAuthor Commented:
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.
0
 
guru_samiCommented:
Ok i see...What you will have to do is ...
1: Create an Event in MasterPage
2: Raise this event from MasterPage after your Authentication if it was successful
3: Add event handler to this event in your ContentPage
4: Call requestApp() method from the event handler

Check this out:
http://dotnetslackers.com/Events/re-23154_Capture_Master_Page_Events_in_Content_Pages_ASP_NET_2_0.aspx

Here is a video link which "might" help but the server seems to be down currently:
http://www.asp.net/general/videos/how-do-i-handle-events-in-master-and-content-pages

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now