Solved

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

Posted on 2011-03-09
10
619 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IIS and Windows Authentication to SQL 21 54
Authentication of Web Services 3 53
Any Third party toll for Integrating chat system on ASP.NET ? 2 34
Runtime Error 2 28
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

16 Experts available now in Live!

Get 1:1 Help Now