initial load only

S

Starbuck

Hi

I want to set a value on the initial load of a asp page and only then.
What I have noticed is that every time I click on my Treeview control all of
the following are called -

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init

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

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit

Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreLoad

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender

So my question is where do I put code that will be fired when the page is
loaded but not every time it is refreshed?

Thanks in advance
 
M

Mark Fitzpatrick

The events will still be called because that is part of the normal ASP.Net
process. What you'll have to do is right before your code, determine if the
page is posting back or not. Put your code that you don't want to repeatedly
fire in a conditional statement and check to see if the Page.IsPostBack
property is true. If so then the page is reloading from a postback and
shouldn't do the code you entered.

Now, this method only handles postbacks. The page doesn't really know if
it's being refreshed unless that refresh posts data back to the server. The
best way to overcome this could be with caching instead. Caching will mean
that the page loads, then keeps itself in memory until some condition is met
to make it fall out of memory or reload (such as placing a timeout or set it
to vary the cache by some parameter). Caching may be what you are looking
for as it has the highest performance.

You'll have to toy with both of these methods to get them just right when
using a control such as a treeview.
 
S

Starbuck

Thanks Mark


--
nivekski
www.kevsbox.com
Mark Fitzpatrick said:
The events will still be called because that is part of the normal ASP.Net
process. What you'll have to do is right before your code, determine if
the page is posting back or not. Put your code that you don't want to
repeatedly fire in a conditional statement and check to see if the
Page.IsPostBack property is true. If so then the page is reloading from a
postback and shouldn't do the code you entered.

Now, this method only handles postbacks. The page doesn't really know if
it's being refreshed unless that refresh posts data back to the server.
The best way to overcome this could be with caching instead. Caching will
mean that the page loads, then keeps itself in memory until some condition
is met to make it fall out of memory or reload (such as placing a timeout
or set it to vary the cache by some parameter). Caching may be what you
are looking for as it has the highest performance.

You'll have to toy with both of these methods to get them just right when
using a control such as a treeview.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top