Home » , » Request for Session time out alert with master page C#,Asp.net at a toz help

Request for Session time out alert with master page C#,Asp.net at a toz help

Written By M.L on சனி, 21 ஏப்ரல், 2012 | ஏப்ரல் 21, 2012

Hi All,
In our web application, there is a requirement to indicate client about session time out.
I have implemented following code which works fine without master page.
The below code is not working in master page as master page loads only once in its session.
======================================================================
<script type="text/javascript">
        var sessionTimeout = "<%= Session.Timeout %>";
        function DisplaySessionTimeout()
        {
            sessionTimeout = sessionTimeout - 1;
           
            //if session is not less than 0
            if (sessionTimeout >= 0)
                //call the function again after 1 minute delay
                window.setTimeout("DisplaySessionTimeout()", 60000);
            else
            {
                //show message box
                alert("Your current Session is over.");
            }
        }
</script>
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //set DisplaySessionTimeout() as the startup script of this page
            Page.ClientScript.RegisterStartupScript(this.GetType(),  "onLoad","DisplaySessionTimeout()", true);
        }
    }
======================================================================
Please send the code for session time out message for a web application with master page.
ANSWER :1


Code Snippet to show “Session Time Out” message in web application which using master pages as well as partial post back.
Add the following code inside the body of master page markup language.
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequestHandler);
//This function will called by every AJAX post back/Partial post back.
function EndRequestHandler(sender, args)
{
     fn1();
}
//This function will reset the time out value when any partial or full post happens.
function fn1()
{
clearTimeout(t);
sessionTout="<%= Session.Timeout %>";
fn2();
}
//This function will call recursively by every 1 minute to reduce the value of Time Out by 1, and alert once reaches threshold value as well as redirect to login/logout page once Time Out value become zero.
function fn2()
{
sessionTout = parseInt(sessionTout) - 1;
if (parseInt(sessionTout) <= 5 && parseInt(sessionTout) > 0)
{           
alert("Your session is idle for long time and will be expired in " + sessionTout + " minutes, Please save your work");                        
}
else if (parseInt(sessionTout) <=0)
      {
alert("Your current session has been expired!, Please login.");
            clearTimeout(t);
            window.location = "../LogOut.aspx";
     }
     
      t=setTimeout('fn2()',60000);
}       
</script>
Add the following code in load event of master.vb code page.
VB.Net
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ShowPopup", "fn1();", True)
C#.Net
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowPopup", "fn1();", True)
This would help iI Guess!!.

Note : Provide your comments by clicking below options! Thanks ! :)

2 comments:

Popular Posts

General Category