Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 11th, 2008, 01:48 PM
Authorized User
Points: 136, Level: 2
Points: 136, Level: 2 Points: 136, Level: 2 Points: 136, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: London Town, , United Kingdom.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Executing a function on every page..

Hi..

If I wanted to keep a record of a logged-in user's visit to a page in a DB Table, how would I include this function within a .NET page?

In classic ASP I might use at the top of each page; record_visit.asp would then contain a call to the DB using a Stored Proc that sends user id, user IP and a datestamp etc. to a user_pages table.

what would be the equivalent in ASP.NET [C#]..? I'm using Master Pages, would I place the code in there? If so, do I need to create the connection object each time?



Hasta Luego..
KingRoon

DogFightClothing. No dogs. No fighting.
http://www.dogfightclothing.com
__________________
Hasta Luego..
KingRoon

DogFightClothing. No dogs. No fighting.
http://www.dogfightclothing.com
  #2 (permalink)  
Old January 11th, 2008, 01:56 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

yep.. you could add that kind of function in the master page.. and yes, you will have to open the connection every time...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #3 (permalink)  
Old January 11th, 2008, 01:59 PM
Authorized User
Points: 136, Level: 2
Points: 136, Level: 2 Points: 136, Level: 2 Points: 136, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: London Town, , United Kingdom.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Gonzalo ;o)

Hasta Luego..
KingRoon

DogFightClothing. No dogs. No fighting.
http://www.dogfightclothing.com
  #4 (permalink)  
Old January 12th, 2008, 12:10 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Another option is to build a base pase class that all your pages will inherit from. That class would contain all common functionality. You pages can still use a master page for the layout. Using a basepage would give you the benefit of keeping application logic in one place while using the master page for the layout. This might be advantageous if you need a page that doesn't use the master page but you still want the logging capability.

-Peter
  #5 (permalink)  
Old January 14th, 2008, 05:19 AM
Authorized User
Points: 136, Level: 2
Points: 136, Level: 2 Points: 136, Level: 2 Points: 136, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: London Town, , United Kingdom.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aha, this is the option I like the sound of, then whenever I need it, I can use the function/class ad hoc hey..

How do I go about writing a class then? Whereabouts is this code kept in the project?

Hasta Luego..
KingRoon

DogFightClothing. No dogs. No fighting.
http://www.dogfightclothing.com
  #6 (permalink)  
Old January 14th, 2008, 11:31 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Depending on what type of project you are using there are two answers:

For a standard VS2005 web site project, you need to create the new class file in your App_Code folder.

For a web application project, you can create it anywhere in the web app project itself or in a supporting assembly project.

Either way, your class will look something like this:
Code:
public abstract class BasePage : System.Web.UI.Page{

   //create protected members for use in the pages
   protected void LogPageHit(){
      //do stuff here
   }
}
You then change the class your pages inherit from:
Code:
public partial class myPage : BasePage {

   //somewhere in a method
   LogPageHit();

}
This would give you good control over when this method is called. You could also have the base page itself call this method so it always gets called for all pages. If you needed to you could create a base page property such as "NoLog" and set that to false if you don't want to log that particular page. Then the base page could check that before executing the logging functionality. This is of course subject to the order in which things happen. If you put the assignment to disable the logging in the page's constructor then it will be turned off before the page ever starts to process so you'd be safe.

-Peter


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a JavaScript Function from XSL IronStar XSLT 5 July 8th, 2010 07:49 PM
XSLT not executing java function(using SAXON) dved XSLT 2 January 15th, 2008 08:46 PM
Executing a java program from an ASP page Anirban Dhar Classic ASP Professional 1 July 2nd, 2006 10:58 AM
Executing a function mepancha SQL Server 2000 0 April 28th, 2005 10:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.