Solved

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

Posted on 2011-03-09
10
606 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
you are using Response.Redirect

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

Expert Comment

by:HainKurt
Comment Utility
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
Comment Utility
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
Comment Utility
or wrap your code with if... end if

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

Author Closing Comment

by:SamSherwin
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a‚Ķ
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall‚Ķ
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

7 Experts available now in Live!

Get 1:1 Help Now