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
675 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now