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

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

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

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
SamSherwin
Asked:
SamSherwin
  • 4
  • 2
  • 2
  • +2
1 Solution
 
gamarrojgqCommented:
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
 
John ClaesCommented:
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
 
HainKurtSr. System AnalystCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Paul JacksonCommented:
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
 
SamSherwinAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
you are using Response.Redirect

and thats why Page1 loads,  calls Page2 then it calls page3
0
 
HainKurtSr. System AnalystCommented:
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
 
gamarrojgqCommented:
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
 
HainKurtSr. System AnalystCommented:
or wrap your code with if... end if

if page.isPostback then
 ... put your code here
end if
0
 
SamSherwinAuthor Commented:
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

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
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now