1

I am creating asp.net session object in Masterpage pageload and again I am checking in content page where session is exists or not. It is not working in initial first load. If I refresh (F5) then I am able to get this.

Master

Dim User As System.Security.Principal.IPrincipal
User = System.Web.HttpContext.Current.User

Dim username As String
username = User.Identity.Name

Try
    lblUsername.Text = "Welcome " & IIf(Not String.IsNullOrEmpty(GetFullName(User.Identity.Name)), GetFullName(User.Identity.Name), User.Identity.Name)

    Session("username") = username
Catch ex As Exception

End Try

ContentPage

  If Not Page.IsPostBack Then
            If Not Session("username") Is Nothing Then
                Dim Clients As List(Of Dim_Client)
                Dim c As New Dim_Client
                Clients = c.GetClients(Session("username").ToString)
                If Clients.Count > 0 Then
                    ddlClients.DataTextField = "Client_Name"
                    ddlClients.DataValueField = "Client_Idx"
                    ddlClients.DataSource = Clients
                    ddlClients.DataBind()


                End If

            End If

        End If
James123
  • 11,184
  • 66
  • 189
  • 343
  • hey Buddy, you should take a look at [the complete lifecycle](http://blogs.thesitedoctor.co.uk/tim/2006/06/30/Complete+Lifecycle+Of+An+ASPNet+Page+And+Controls.aspx) of an ASP.Net page. – BrOSs Apr 11 '13 at 15:59

3 Answers3

1

I think your content page code may be executing before your masterpage code. In what methods/events do you have these statements?

Edit - See here: asp-net-masterpage-load-first-or-page-load-first. Content page page_load fires before master page page_load

Another Edit: For a solution, try moving the master page code to the init handler.

Community
  • 1
  • 1
Jason P
  • 26,984
  • 3
  • 31
  • 45
0

Check this page out. http://msdn.microsoft.com/en-us/library/dct97kc3(v=vs.100).aspx

Your content page page_load event is being called before the master page page_load event

Even more information here: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

Gage
  • 7,365
  • 9
  • 47
  • 77
0

You're using Windows Authentication, do this work in the Session_Start of the application in the Global.asax - if you don't have one in your project you can add it via Add New Item on the project.

Do this for two reasons, first because it only needs done once per session, but second because it will then be available in your content page and can be removed from the master page.

Based on your code you'll leave the label work in the master page, but the work to gather and set the user name in session, do that in the application class.

I'd try and provide a code example but I'm answering this from my phone.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232