?
Solved

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

Posted on 2011-03-09
10
Medium Priority
?
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 59

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 59

Expert Comment

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

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

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 2000 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 59

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

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.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

719 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