Run script at entry of each page

T

tshad

Is there a way to run a script or function on entry of each page without
having to put a call in each page?

Periodically, I find something I want to do each time a page is entered and
I have to go into each page and add a line or 2 in my asp.net code. That's
a lot of pages and I could easily miss one.

For example, I have a user object that I built that I just added a field -
LastPageVisited. This was necessary because, apparently, you can't depend
on the referrer to have the last page. I need this to tell me where I came
from in my site or if I even came from a page in my site or whether a user
just typed the URL into the address line of the browser.

So now I just add the line

Session("User").LastPageVisited = "This Page".

But I have to add this to about 150 pages.

If I make a change to this, I need to make the change to each 150 pages.

Thanks,

Tom
 
C

Chris Botha

Best I would say is to create a page called say BaseForm.aspx and in its
Page_Load you code your Session("User").LastPageVisited = "This Page". Then
in the code-behind of the other forms change the "Inherits
System.Web.UI.Page" to "Inherits BaseForm". Bad news is you still are going
to change 150 pages to derive from BaseForm, but if your code changes it
changes in the base only and you don't have to update the session variable
in the Page_Load of the derived forms.
 
T

tshad

Chris Botha said:
Best I would say is to create a page called say BaseForm.aspx and in its
Page_Load you code your Session("User").LastPageVisited = "This Page".
Then in the code-behind of the other forms change the "Inherits
System.Web.UI.Page" to "Inherits BaseForm". Bad news is you still are
going to change 150 pages to derive from BaseForm, but if your code
changes it changes in the base only and you don't have to update the
session variable in the Page_Load of the derived forms.

Problem with this is I am not using Code-Behind pages.

Tom
 
P

Peter Rilling

How about creating an HttpHandler, or adding code to the
Application_BeginRequest in the global.asax.
 
G

Guest

Tom,

You don't want an HttpHandler, you want an HttpModule. An HttpModule is
like a filter that you can add to your app. Simply put, on each request you
will have functions that get called before (or after) the page is actually
executed. You can add your code here, because the Session and the Request
(for getting the url) are both available. You will only have to code it once
and then add it to your web config. Your question, "Is there a way to run a
script or function on entry of each page without having to put a call in each
page?" is what they added modules for, more or less.

Tim
 
R

Randall Parker

Tim,

Is an HttpModule a good place to enforce requiring all users to log in before viewing
certain pages?
 
R

Randall Parker

Chris,

If one has a site that one is going to require people to log into then is the base
page approach a good way to make sure that a user has logged in before he gets to
see any pages?

Could one just do a Response.Redirect call in the base's Page_Load
 
C

Chris Botha

Hi Randall, yes, it will work, you may want to do it as
Response.Clear()
Response.Redirect(...
Response.End()

Also have a look at Forms Authentication, it is more involved, sort of does
the same and has more features.
 
R

Randall Parker

Chris,

Dumb newbie question: Why the Response.Clear before the Redirect? I've done Redirect
by itself and that seems to work.
 
C

Chris Botha

Superstition, or extra luck, I guess :)

Randall Parker said:
Chris,

Dumb newbie question: Why the Response.Clear before the Redirect? I've
done Redirect by itself and that seems to work.
 
G

Guest

Hi Randall,

If you are not using Forms Authentication, An HttpModule is a great way to
go for requiring login. You just hook into the Page right before it starts
to process its request. At that point you check your session variable or
whatever you are using. If it's not set you redirect to login page.

I suggest using FormsAuthentication, it's only a few lines of code and
handles everything for you.

Some people call Response.Clear() before Response.Redirect() because if you
are not buffering your Response, you can not Redirect it if you have started
to write the page. Buffering is turned on by default in ASP.NET, so I
haven't had to use this since classic ASP.

Tim
 
T

tshad

timkling said:
Tom,

You don't want an HttpHandler, you want an HttpModule. An HttpModule is
like a filter that you can add to your app. Simply put, on each request
you
will have functions that get called before (or after) the page is actually
executed. You can add your code here, because the Session and the Request
(for getting the url) are both available. You will only have to code it
once
and then add it to your web config. Your question, "Is there a way to run
a
script or function on entry of each page without having to put a call in
each
page?" is what they added modules for, more or less.

Sounds like what I am looking for, almost.

I was looking around and can't seem to find out how to make it work at the
beginning of my page only at Page_Load time.

In my example below, I have an user object that has a string which is
LastPageVisited. I want to update this each time I go to a page in my site.
But I only want to do it at about the Page_Load/not Postback section.

I can't seem to find anything that says I can do that. I did see someone
trying to do the same type of thing but was having the same problem I was
talking about. His code was being run both at initial (Page_Load/not
Postback) and Postbacks.

Thanks,

Tom
 
G

Guest

Tom,
You are definitely right about the fact that the HttpModule and the Page
load are happening at different points.

If you want to put it in page load, you will have to resort to all the
coding. There is no easy way around that. If you were to use an HttpModule,
you can hook the Application's "PreRequestHandlerExecute" event, which will
let you into the request just before the page load happens (more or less).
You will have the session available to set the property.

From your example, I don't think it would matter if you set the property
every time the page is called even if it is PostBack, however it is possible
to find out if it is a PostBack, but you have to find out manually. Don't
try to check the IsPostBack property at this point as it should always return
false.

If you absolutely have to know if it IsPostBack, you have two options:
1. Put the code (to get the value the property should be set to) in a
standalone class with a static method. In Page_Load (on every page) set the
property to the result of the method call (this limits the amount of code you
put in each page to one line... if you have to change the code to get that
value, you only change it in one place, not in hundreds of pages).
2. You can find out if the page is PostBack manually in an HttpModule,
before the page is executed but this could get a little messy. I would
advise against it. (You can check page IsPostBack after the page executes in
an HttpModule, but I assume that is not what you want).

I can help you with specific code if you want.
Tim
 

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

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top