Total members 11889 |It is currently Fri Mar 29, 2024 1:17 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:
Newbie
User avatar Posts: 3
Have thanks: 0 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.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 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.


Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

Thanks! was looking for same problem. :beg: :Blackeye:


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

yaa...really thanks....
from so many days i am also looking for dis solution...
:gOOd:


Author:
Newbie
User avatar Posts: 27
Have thanks: 1 time
Post new topic Reply to topic  [ 5 posts ] 

  Related Posts  to : how to write code message to user before session will expire
 i need code for identifying web user session     -  
 Default Expire time for Session JAVA     -  
 stick message even when user scrolls down or up     -  
 How to write a code for sorting array of 100 number in C++     -  
 How to write C++ code that defines array of size 24 then;     -  
 JMS Message Consumer Example     -  
 How to Show a message before the login ?     -  
 Popup message without using javascript     -  
 Trace soap message     -  
 Text message board RPG     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com