Solved

asp.net page_load events firing on all pages at start up

Posted on 2011-03-09
10
629 Views
Last Modified: 2012-08-13
Hi There

Just starting out with asp.net 3.5 VS2008, vb.net
I've got a web site project with 3 pages, default,aspx, page2.aspx and page3.aspx.

default.aspx links to page2, page 2 to page 3, nothing fancy just a button link.

When I run the web site (asp.net development server) the page_load event on all three pages fires.  Is this how asp.net works?

I would have expected the page_load event to fire on default.aspx when the project starts, then once the link button is pressed the page_load event on page2 to fire etc. etc.

The page_load event also fires correctly when the link button to that page is clicked.

Any help greatly appreciated.

Sam
0
Comment
Question by:SamSherwin
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35084473
Hi,

This will mean that the 3 pages opens up? or it opens one by one and finally stop in the third one?

The normal behaivor is that the page_load event will fire only for the page you are visiting, did you Default.aspx page is the startup page?

Are you using  an Inheritance class for the 3 pages?
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35084543
Sam,

The page_Load is normaly only fired when the page is loaded.
can you send me some Code snippits from you initialisation of the 3 pages and the Page_load from the default.aspx ??

regards

poor beggar
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35084637
some piece is missing... when it starts it only loads page1.aspx and page_load of page1 will fire initially...
then when you click buttons, page2 or page3 will load and page_load of page2 or page3 will fire...

maybe you are doing something weird in page_load of page1

post the code...
0
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.

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35084694
The page_load will also fire on postback, so depending on how your link button is set up i.e. is it server-side? when the link is clicked the page-load for default.aspx will fire as well as the page load for page 2. Then when you click on think button for page 2 and it posts back then the page_load for page 2 will fire and the page_load for page 3.
0
 

Author Comment

by:SamSherwin
ID: 35084713
Below is the page_load events for all 3 pages.
The firing order is default, page3 then page2

I'm just reading up on partial classes, is this perhaps something to do with it?

--------------------- Default --------------------

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public objClient As clsClient
    Public objBranch As clsClient

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim GroupID As Long


        If Request.Form("txtCompanyName") <> "" Then
            GroupID = ValidateUser()
            If GroupID > 0 Then
                labStatus.ForeColor = Drawing.Color.Green
                labStatus.Text = "Loggin In, Please Wait"
                Response.Redirect("page2.aspx")
            Else
                labStatus.ForeColor = Drawing.Color.Red
                labStatus.Text = "Invalid Login Details!"
            End If
        End If
    End Sub

End Class





------------- PAGE 3 --------------------
Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
    Inherits System.Web.UI.Page


    Protected Sub Page3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim BranchID As Long

        BranchID = Request.QueryString("rid")

        Try

            objBranch = New clsClient
            If Application("ClientID") > 0 Then
                objBranch.LoadClient(BranchID)
                'Display Folders

                labWelcome3.Text = "Welcome " & objBranch.CompanyName
            End If
        Catch
        End Try

    End Sub

End Class





---------------- PAGE 2 ---------------------


Partial Public Class _Default



    Inherits System.Web.UI.Page

    Private Sub _Page2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            'objClient = New clsClient
            If Application("ClientID") > 0 Then
                objClient = New clsClient
                objClient.LoadClient(Application("ClientID"))
                DisplayFolders()

                labWelcome.Text = "Welcome " & objClient.CompanyName
            Else
                'Response.Redirect("default.aspx")
            End If
        Catch ex As Exception
            Response.Write("Error: " & ex.Message)
        End Try
    End Sub

End Class


regards

Sam
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35084781
you are using Response.Redirect

and thats why Page1 loads,  calls Page2 then it calls page3
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35084814
move this code to button_click event, leave page_load empty...


Dim GroupID As Long


        If Request.Form("txtCompanyName") <> "" Then
            GroupID = ValidateUser()
            If GroupID > 0 Then
                labStatus.ForeColor = Drawing.Color.Green
                labStatus.Text = "Loggin In, Please Wait"
                Response.Redirect("page2.aspx")
            Else
                labStatus.ForeColor = Drawing.Color.Red
                labStatus.Text = "Invalid Login Details!"
            End If
        End If

Open in new window

0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35084819
Ok that will be your problem, since each 3 pages are "PARTIAL" of the same class, _Defatult

Partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.

So you have to change the class for each page
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35084828
or wrap your code with if... end if

if page.isPostback then
 ... put your code here
end if
0
 

Author Closing Comment

by:SamSherwin
ID: 35084881
Thanks for the fast and accurate feedback,

As I was beginning to suspect, after reading about partial classes, that was the problem.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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