Solved

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

Posted on 2010-11-09
8
677 Views
Last Modified: 2012-05-10
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
Comment
Question by:CitySec
  • 4
  • 3
8 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 34096123
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
 
LVL 11

Expert Comment

by:azarc3
ID: 34096153
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
 
LVL 2

Author Comment

by:CitySec
ID: 34096181
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 41

Expert Comment

by:guru_sami
ID: 34096223
Ok how are you setting forms authentication cookie?
May be we want to see some code...
0
 
LVL 2

Author Comment

by:CitySec
ID: 34096929
I'm not, I'm actually using a session variable instead.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34097152
Can we see some code?
0
 
LVL 2

Author Comment

by:CitySec
ID: 34097436
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 34097721
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question