?
Solved

ASPX page - ensure a task is done just once

Posted on 2003-03-22
13
Medium Priority
?
199 Views
Last Modified: 2010-04-15
Hi,

I have an asp.net page written in C#.  What I want to do is perform a certain task *once*, i.e. when a user first views a page on my site, and not do that task again whilst it is the same user or same browser window if you get me.

The purpose of my asp.net page is to simply log information about who visits my site and what page they look at (this is for statistical analysis), then redirects the user to the appropriate page within my site.

The *first time* someone visits my site however I want to recieve an email (yes I know it may sound sad) informing me of this.  But since it is the same asp.net script that is runs each time a user requests a page, I do not want to be recieving an email for every page they look at !

So how can I achieve this ?  I have read about Sessions and Cookies but do not know which is better.

I also think I may have problems with the Session object as I am requesting pages over two different web servers: the asp.net script is on a different server to the actual site content (images and html).

Can someone give me advice and/or code please ??

Thanks in advance.
0
Comment
Question by:mrwad99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 1

Accepted Solution

by:
Commodus2 earned 120 total points
ID: 8196358
Mayby it is an idea to use Session's. You can check it's existance and do the job once when it not exists, create session var, and when the user refeshes or visits the page in the same browse session, it will not be executed

in page_load for instance:

if (Session["alreadyDone"] != null) {
     Response.Write("doing it once...");//your code can be placed here
     Session.Add("alreadyDone", true);
}

I hope i understoud the question right and this is of some value to you.
0
 

Expert Comment

by:Guzzi
ID: 8209224
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//what u whant to do when page is loaded first time
}
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8209257
Hi Commodus2 and Guzzi.

Thank you for your feedback.

Commodus2 I will look further into sessions and come back with any problems.

Guzzi,

What you say about the Page_Load method is correct, but would not work for my script.  The same script is executed on the page load method for every page on my site the user looks at.  The purpose of the script is simply to log the page viewed before redirecting the user to the appropriate page within my site.  

eg <a href=Logger.aspx?redirPage=page2.html>Click to go to Page 2 </a href>.  As a result, no information will be resubmitted to the page, so (!(Page.IsPostBack)) will always return true.

Now the logging (database connection and data entry) has to be done in the Page_Load method (or a function called from this) hence I need some way of distiguishing if the page viewed is the first page the user has looked at.  If it is, send the email otherwise do not.  I figured sessions or cookies were to way to go.

hope this clarifies my needs.

Thanks again both for the input.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Guzzi
ID: 8209496
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//what u whant to do when page is loaded first time
}
0
 

Expert Comment

by:Guzzi
ID: 8209571
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//what u whant to do when page is loaded first time
}
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8233549
Commodus2

I have looked into sessions and hit a problem.

On my page are links that reload my aspx page, except with different parameters, so the data displayed is different.

eg

display.aspx?recNo=10

display.aspx?recNo=8

In the Page_Load method of my display.aspx page I am outputting the SessionID.

However, I find that clicking on a link redisplays the page ok, but also changes the Session ID.  

I did not think this was the purpose of the session object, and was only using it as I want to keep a track of the the queries one user runs by logging the query run against the session id.  That way I can display all the records with identical session ids and be confident that these queries were run by the same user.

What am I doing wrong ?
0
 
LVL 1

Expert Comment

by:Commodus2
ID: 8236887
Can you post your Page_Load code? Did you update the session variable with the recNo from the QueryString?
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8237132
Ok, this is a cut down listing of my whole page.  Very simple.  Clicking the link changes the SessionID value output.

//File: Test2.aspx

<%@ Page Language="C#" %>
<script runat="server">

    protected void Page_Load (Object src, EventArgs e)
    {
        Response.Write("recNo is" + Request.QueryString["recNo"]);
        Response.Write("<BR>SessionID is " +Session.SessionID);
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
    <A href = "Test2.aspx?recNo=24242">Redirect</A href>
        <!-- Insert content here -->
    </form>
</body>
</html>
0
 
LVL 1

Expert Comment

by:Commodus2
ID: 8237230
hm... after some testing I cannot reproduce your code's behavior. My sessionid stays the same.

I copy pasted your code and I always got the same id. Mayby it is something else in your code. Try putting it back in the cut down version step by step and see when the sessionid changes.

What framework version doe you have? I have 1.1
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8237258
This is crazy.  

I first thought it must be something to do with my running the page on my machine locally, so I uploaded the page to my service provider and I still get the session ID changing !

Check

http://www10.brinkster.com/dbillingham/Test2.aspx?

recNo=24242 for me please.
0
 
LVL 1

Expert Comment

by:Commodus2
ID: 8237309
I see it changing also

That is very strange. Can I see this code? or is it copyrighted?
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8237432
No it is not copyrighted and the code I have posted as Test2.aspx is the code in its entirity.

I have researched this on the Internet and discovered that unless a value is stored in the Session a new one will be created, hence new ID.

I have got around this problem by storing a placebo value in the Session and found that the SessionID does not change thereafter.

This still does not solve the mystery as to how the SessionID changes on my machine locally but not on yours, but it was apparent that a problem lay in my code when we both found that the SessionID changed when running remotely.

With the line

Session["Name"] = "Me";

added,

http://www10.brinkster.com/dbillingham/Test2.aspx?recNo=24242

now works correctly.

I will of course do as advised in your first post to solve my problem of only performing a task once.

By the way, I am right in thinking that Sessions will not be blocked even if the user is disallowing all cookies aren't I ?

Thank you for the advice.
-- DJ
0
 
LVL 1

Expert Comment

by:Commodus2
ID: 8237448
I am not sure about that.. Sessions being available when user has disabled cookies.. I';ve read something about that in the msdn docs.

Thanks.

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

764 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