This is a migrated thread and some comments may be shown as answers.

session timeout causes postback?

1 Answer 452 Views
Notification
This is a migrated thread and some comments may be shown as answers.
K
Top achievements
Rank 1
K asked on 05 Oct 2011, 05:10 AM
From what I understand, the way the demo is set up, when the 'Continue Session' button is clicked, a callback should reset the session on the server, meaning the entire page should not be refreshed. And indeed I do not notice a page refresh on the demo. Yet, my page refreshes every time the session is continued. Is there a way to reset the session without this page refresh?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadNotificationSolution.Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
    <head runat="server">
        <title></title>
        <telerik:RadCodeBlock runat="server">
            <script type="text/javascript">
                var timeLeftCounter = null;
                var seconds = 60;
                //a flag to ease the logic which determines whether to redirect the user
                //will not redirect if the RadButton is clicked
                var toRedirect = true;
 
                //stop timers for UI
                function stopTimer(timer) {
                    clearInterval(this[timer]);
                    this[timer] = null;
                };
 
                //reset timers for UI
                function resetTimer(timer, func, interval) {
                    this.stopTimer(timer);
                    this[timer] = setInterval(Function.createDelegate(this, func), interval);
                };
 
                function OnClientShowing(sender, args) {
                    console.log("OnClientShowing()");
                    resetTimer("timeLeftCounter", UpdateTimeLabel, 1000);
                    //raise the flag again so that a redirect will occur if the notification autocloses
                    toRedirect = true;
                }
 
                function OnClientHiding(sender, args) {
                    if (toRedirect) {
                        window.location.href = sender.get_value();
                    }
                }
 
                //update the text in the label in RadNotification
                //this could also be done automatically by using UpdateInterval. However, this will cause callbacks [which is the second best solution than javascript] on every second that is being count
                function UpdateTimeLabel(toReset) {
                    console.log("UpdateTimeLabel():", toReset);
                    var sessionExpired = (seconds == 0);
                    if (sessionExpired) {
                        stopTimer("timeLeftCounter");
                        //redirect to session expired page - simply take the url which RadNotification sent from the server to the client as value
                        window.location.href = $find("RadNotificationSessionTimeout").get_value();
                    } else {
                        var timeLbl = $get("timeLbl");
                        timeLbl.innerHTML = seconds--;
                    }
                }
 
                function ContinueSession() {
                    console.log("ContinueSession()");
                    var notification = $find("RadNotificationSessionTimeout");
                    toRedirect = false;
                    notification.update();
                    notification.hide();
                }
            </script>
        </telerik:RadCodeBlock>
    </head>
    <body>
        <form id="form1" runat="server">
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <div>
                <asp:Literal runat="server" ID="literalContent"></asp:Literal>
                <telerik:RadNotification ID="RadNotificationSessionTimeout" runat="server" ClientIDMode="Static" Position="Center" Width="240"
                                         Height="200" OnCallbackUpdate="OnCallbackUpdate" Value="/Logout.aspx?timeout=true" AutoCloseDelay="60000"
                                         Title="Stay Signed In?" Skin="Office2010Blue" EnableRoundedCorners="true" OnClientShowing="OnClientShowing" OnClientHiding="OnClientHiding" LoadContentOn="PageLoad" ShowCloseButton="False" >
                    <ContentTemplate>
                        <div class="notificationContent">Time remaining:  <span id="timeLbl">60</span>
                            <%--<telerik:RadButton Skin="Office2007" ID="continueSession" runat="server" Text="Continue Your Session"
                                                Style="margin-top: 10px;" AutoPostBack="false" OnClientClicked="ContinueSession">
                            </telerik:RadButton>--%>
                            <asp:Button ID="continueSession" runat="server" Text="Continue Your Session"
                                        Style="margin-top: 10px;" AutoPostBack="false" OnClientClicked="ContinueSession">
                            </asp:Button>
                        </div>
                    </ContentTemplate>
                </telerik:RadNotification>
            </div>
        </form>
    </body>
</html>

using System;
using Telerik.Web.UI;
 
namespace RadNotificationSolution
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Random random = new Random();
            literalContent.Text = random.Next().ToString();
 
            // Session Timeout Config
            if (!IsPostBack)
            {
                //set the expire timeout for the session
                Session.Timeout = 2;
                //configure the notification to automatically show 1 min before session expiration
                RadNotificationSessionTimeout.ShowInterval = (Session.Timeout - 1) * 60000;
                //set the redirect url as a value for an easier and faster extraction in on the client
                RadNotificationSessionTimeout.Value = Page.ResolveClientUrl("Logout.aspx?timeout=true");
            }
        }
 
        protected void OnCallbackUpdate(object sender, RadNotificationEventArgs e)
        {
 
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 06 Oct 2011, 09:09 AM
Hello,

Let me start by stating the the ClientIDMode property's Static value is not supported by the RadControls for ASP.NET AJAX. They are mostly complex HTML structures and many of them have content templates which are INaming containers. Therefore the ClientIDMode should be left to Auto in order for them to function correctly. This approach is advised by Microsoft as well for INaming containers.

I also advise that you take the exact code from the demo and build on top of it, as I see some other properties set.

Your actual issue (the full postback) stems from the way you have attached the handler to the button  - you are using a regular asp button and not a RadButton, so it has no AutoPostBack property and its client-side click event is the OnClientClick property, which requires the return false; statement to cancel the postback:
<asp:Button ID="continueSession" runat="server" Text="Continue Your Session"
            Style="margin-top: 10px;" OnClientClick="ContinueSession(); return false;">



Regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Notification
Asked by
K
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or