Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Page_Load firing every time

Status
Not open for further replies.

learningguy

Programmer
Sep 25, 2010
3
US
Hi, all,

First post here. I'm new to ASP.NET (but experienced in .NET), and trying to mock up my first project.

It contains several UpdatePanels that are set to Conditional, ChildControlTriggers = false, and have another control's default event set as the update trigger.

The problem I'm having, which I imagine is very obvious, is that every time anything triggers a postback, all the member variables of the _Default class are recreated and the Page_Load event is run again.

This is happening even for the controls that are only supposed to trigger a partial page postback. But I don't want it to happen at all after the initial page load!

I thought ASP.NET kept state for you? Why is it recreating everything every time?

Do I need to create my session variables in a separate class?
 
I did do so (created a SessionVars static class with an IsLoaded flag), and it got me going, but is that the normal way to do it?
 
learn the webforms page lifecycle. this will answer your questions pertaining to why webforms works the way it does.

One of the more important concepts to understand is that the web is stateless. this is a drastic departure from desktop applications which have state. the webforms html engine attempts to create the illusion of state via viewstate and a page life cycle.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
if you put something like this in your Page_Load then it will only fire the first time the page is visited instead of each "Postback"

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
         *CODE YOU WANT ONLY ON FIRST VISIT* (not PostBacks)
        END IF
    End Sub

That can also be achieved by:

If Page.IsPostBack = False
If Me.IsPostBack = False

You will end up using that alot of the page does postbacks. But with UpdatePanels you can avoid a lot of unnecessary page loads.
 
You will end up using that alot of the page does postbacks. But with UpdatePanels you can avoid a lot of unnecessary page loads.
it would be beneficial to understand how the control works. an update panel simply wraps a request with ajax. webforms still needs to go through the entire page life cycle to process the request. update panels work well to retrofit existing webforms with ajax. for greenfield functionality, update panels should be avoided. if you want/need ajax calls use ajax to send/get json objects, not html. jquery makes this dead simple.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the helpful links, jmeckley, and thanks everyone else, as soon as I saw IsPostBack I did the forehead slap :D It was on the tip of my brain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top