Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 3:23 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 5 posts ] 
Author Question
 Question subject: how to write code message to user before session will expire
PostPosted: Tue Dec 30, 2008 2:45 am 
Offline
Newbie
User avatar

Joined: Tue Dec 30, 2008 12:55 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time

Hi,
Would you please give me exaple of how to code (web application - vb.net for 2003 and where ) time controls and to popup a message to user and redirect to login page when the session times out..
Thank you very much


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: how to write code message to user before session will expire
PostPosted: Tue Dec 30, 2008 7:01 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time
i found this in some blog :
Session.Timeout means that after how much time the user's session will expire and the user will not be able to access the items in the Session object. By default the Session.Timeout is 20 minutes. You can change this through the web.config or the page level code. Let's see a small example:
Code:
Session.Timeout = 1;

Session["Name"] = "Session.Timeout means that after how much time the user's session will expire and the user will not be able to access the items in the Session object. By default the Session.Timeout is 20 minutes. You can change this through the web.config or the page level code. Let's see a small example:

Session.Timeout = 1;

Session["Name"] = "Tomas andro";

In the above code I am setting the Session.Timeout to "1" minute. After that I put "Tomas andro" in the Session object. This means that if you don't make request to the server and sit idle then after 1 minute you won't be able to access the "Name" key in the Session bag. A good scenario will be a user filling out a long form and you store the values of the form in a Session object. The user fills only couple of fields and then sit idle for 25 minutes keeping in mind that the Session timeout is 20 minutes. Now, if the user tries to retrieve something from the Session object after 25 minutes it will throw the ArgumentNullException as the Session object has been removed.

The question is how do we renew the Session and how do we notify the user that his session is about to expire. The idea is to use JavaScript and notify the user 10 seconds before the Session is about to expire.

First, here is the code from the TestPage.aspx.cs:

public partial class TestPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["Name"] = "Tomas andro";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["Name"] == null)
                throw new ArgumentNullException("Session[Name] is null");
        }
    }

With the click of a button I insert my name "Tomas andro" into the Session object. The Session.Timeout is 1 minute and is configured in the web.config file.

Here is the code for the BasePage.cs:

public class BasePage : System.Web.UI.Page
    {

        public BasePage()
        {
            this.Load += new EventHandler(BasePage_Load);
        }

        void BasePage_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(BasePage));
            InjectTimerScript();
        }

        [AjaxPro.AjaxMethod]
        public void UpdateSession()
        {
           
        }

        public void InjectTimerScript()
        {       
            // set the time for 10 seconds less then the expiration time!

            var t = Session.Timeout * 50 * 1000;         
            //var t = 1000;
            string script = String.Empty;

            if (!ClientScript.IsClientScriptBlockRegistered("Timer"))
            {
                script = "function refreshSession() { document.getElementById(\"btnRefreshSession\").style.display ='none'; document.getElementById(\"divTimeOutMessage\").innerHTML = \"\";  DemoWatiN.BasePage.UpdateSession(); } function notifyTimeOut() { document.getElementById(\"btnRefreshSession\").style.display ='block'; document.getElementById(\"divTimeOutMessage\").innerHTML = \" Your Session is about to expire! \"   }  setInterval(\"notifyTimeOut()\"," + t + ")";

                ClientScript.RegisterClientScriptBlock(this.GetType(), "Timer", script, true);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<div id=\"divTimeOutMessage\"></div><input type=\"button\" id=\"btnRefreshSession\" style=\"display:none\" value=\"Refresh Session\" onclick=\"refreshSession()\" />");
            base.Render(writer);
           
        }

When the page loads I register the page to use AJAXPRO.NET library and inject the timer script. The InjectTimerScript method injects the setInterval method which fires 10 seconds before the Session timeout. It also presents the user with a button "Refresh Session". When the "Refresh Session" button is clicked an Ajax call is made to the "UpdateSession" method. There is NO code inside the UpdateSession method. The call/request renews the session for another 1 minute.

This technique helps us to notify the users that their session is about to expire so if they like to continue they must refresh/renew their session. ";


In the above code I am setting the Session.Timeout to "1" minute. After that I put "Tomas andro" in the Session object. This means that if you don't make request to the server and sit idle then after 1 minute you won't be able to access the "Name" key in the Session bag. A good scenario will be a user filling out a long form and you store the values of the form in a Session object. The user fills only couple of fields and then sit idle for 25 minutes keeping in mind that the Session timeout is 20 minutes. Now, if the user tries to retrieve something from the Session object after 25 minutes it will throw the ArgumentNullException as the Session object has been removed.

The question is how do we renew the Session and how do we notify the user that his session is about to expire. The idea is to use JavaScript and notify the user 10 seconds before the Session is about to expire.

First, here is the code from the TestPage.aspx.cs:
Code:
public partial class TestPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["Name"] = "Tomas andro";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["Name"] == null)
                throw new ArgumentNullException("Session[Name] is null");
        }
    }

With the click of a button I insert my name "Tomas andro" into the Session object. The Session.Timeout is 1 minute and is configured in the web.config file.

Here is the code for the BasePage.cs:
Code:
public class BasePage : System.Web.UI.Page
    {

        public BasePage()
        {
            this.Load += new EventHandler(BasePage_Load);
        }

        void BasePage_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(BasePage));
            InjectTimerScript();
        }

        [AjaxPro.AjaxMethod]
        public void UpdateSession()
        {
           
        }

        public void InjectTimerScript()
        {       
            // set the time for 10 seconds less then the expiration time!

            var t = Session.Timeout * 50 * 1000;         
            //var t = 1000;
            string script = String.Empty;

            if (!ClientScript.IsClientScriptBlockRegistered("Timer"))
            {
                script = "function refreshSession() { document.getElementById(\"btnRefreshSession\").style.display ='none'; document.getElementById(\"divTimeOutMessage\").innerHTML = \"\";  DemoWatiN.BasePage.UpdateSession(); } function notifyTimeOut() { document.getElementById(\"btnRefreshSession\").style.display ='block'; document.getElementById(\"divTimeOutMessage\").innerHTML = \" Your Session is about to expire! \"   }  setInterval(\"notifyTimeOut()\"," + t + ")";

                ClientScript.RegisterClientScriptBlock(this.GetType(), "Timer", script, true);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<div id=\"divTimeOutMessage\"></div><input type=\"button\" id=\"btnRefreshSession\" style=\"display:none\" value=\"Refresh Session\" onclick=\"refreshSession()\" />");
            base.Render(writer);
           
        }

When the page loads I register the page to use AJAXPRO.NET library and inject the timer script. The InjectTimerScript method injects the setInterval method which fires 10 seconds before the Session timeout. It also presents the user with a button "Refresh Session". When the "Refresh Session" button is clicked an Ajax call is made to the "UpdateSession" method. There is NO code inside the UpdateSession method. The call/request renews the session for another 1 minute.

This technique helps us to notify the users that their session is about to expire so if they like to continue they must refresh/renew their session.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: how to write code message to user before session will expire
PostPosted: Tue Jan 13, 2009 5:12 am 
Offline
Newbie
User avatar

Joined: Tue Dec 30, 2008 12:55 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
Thank you for answer.
I am not using AJAC, so I find next solution.
1. In the Page_Load function (can be Master page too) I wrote next code :
Response.AppendHeader("Refresh", Convert.ToString(Session.Timeout * 1) & ";
URL=Timeout.aspx")
2. In the Timeout.aspx after before </head > I wrote next code:
<script type="text/javascript" language="javascript">
function endSession()
{
alert("Your session has expired. You will be redirected to the login page.");
url = '<%=mstrLoginURL %>';
document.location=url;
}
registerLoadTask('endSession()')
</script>

3) In the Timeout.aspx.vb in the Page_Load function I wrote next script:
If Not IsPostBack Then

mstrLoginURL = ResolveUrl("LogIn.aspx")
End If
So it is working.
Thank you for your suggestion. I will try your suggestion when I will start to use VB.Net 2008.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: how to write code message to user before session will expire
PostPosted: Mon Aug 17, 2009 2:21 pm 
Offline
Newbie
User avatar

Joined: Mon Aug 17, 2009 2:08 pm
Posts: 2
Has thanked: 0 time
Have thanks: 0 time
Thanks! was looking for same problem. :beg: :Blackeye:


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: how to write code message to user before session will expire
PostPosted: Wed Sep 22, 2010 10:37 am 
Offline
Newbie
User avatar

Joined: Tue Sep 07, 2010 9:18 am
Posts: 30
Has thanked: 0 time
Have thanks: 1 time
yaa...really thanks....
from so many days i am also looking for dis solution...
:gOOd:

_________________
Robinetterie industrielle


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 5 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 java project code  Java  Anonymous  0
 Read your gmail using Java code  Java examples  msi_333  5
 What's wrong with my code?  Java  Anonymous  3
 project source code in java  Java  Anonymous  0
 Ajax Source code to Suggest application with JSP Server side  AJAX  msi_333  5

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team