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

Generate dynamic notification and display Session timeout popup

1 Answer 682 Views
Notification
This is a migrated thread and some comments may be shown as answers.
Dipen
Top achievements
Rank 1
Dipen asked on 28 Apr 2017, 12:16 PM

Dear Telerik Team,

We would like to use rad notification session timeout feature in my code but we have different project scenario.
We have master page in project but Form tag is inside content page so we are not able to add notification control in master page.
We don't want to add rad notification on each page.
We have created a dynamic control on page load but getting error on continue session button.

Master Page ASPX
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="SampleApplicationSession.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
     <div class="clockSession">
            <div class="contSession">
                <div class="sesseionExpire">
                    Your Session will expire in minutes</div>
                <div class="showNotification">
                    Notification will be shown in:</div>
                <div class="timeRemain">
                    <span class="timeSeconds"><span id="mainLbl">60 </span></span>seconds</div>
            </div>
        </div>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

Master page CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SampleApplicationSession
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
WebForm1.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SampleApplicationSession
{
    public partial class WebForm1 :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Userid"] = "Test";

        }
    }
}

WebForm ASPX

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SampleApplicationSession.WebForm1" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>




<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">


   

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 
<form id="GlmasterViewForm" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Assembly="Telerik.Web.UI" 
            Name="Telerik.Web.UI.Common.Core.js">
        </asp:ScriptReference>
        <asp:ScriptReference Assembly="Telerik.Web.UI" 
            Name="Telerik.Web.UI.Common.jQuery.js">
        </asp:ScriptReference>
        <asp:ScriptReference Assembly="Telerik.Web.UI" 
            Name="Telerik.Web.UI.Common.jQueryInclude.js">
        </asp:ScriptReference>
    </Scripts>
</telerik:RadScriptManager>
</form>

</asp:Content>


BaseFile
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


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

        RadNotification SessionTimeOutNotification = new RadNotification();

        protected void OnCallbackUpdate(object sender, RadNotificationEventArgs e)
        {

        }
        protected override void OnLoad(EventArgs e)
        {
            try
            {

                if (!Page.IsPostBack)
                {
      



                    SessionTimeOutNotification.ContentContainer.CssClass = "";
                    SessionTimeOutNotification.ID = "TestRad";
                    SessionTimeOutNotification.RenderMode = RenderMode.Lightweight;
                    //SessionTimeOutNotification.Position = NotificationPosition.TopCenter;
                    //rad.Width = 190;
                    SessionTimeOutNotification.Title = "Continue Your Session";
                    SessionTimeOutNotification.EnableRoundedCorners = true;
                    SessionTimeOutNotification.ShowCloseButton = false;
                    SessionTimeOutNotification.KeepOnMouseOver = false;
                    SessionTimeOutNotification.LoadContentOn = NotificationLoad.PageLoad;
                    //SessionTimeOutNotification.AutoCloseDelay = 6000;
                    
                   
                    //configure the notification to automatically show 1 min before session expiration
                    SessionTimeOutNotification.ShowInterval = (Session.Timeout - (Session.Timeout - 1)) * 60 * 1000;
                    SessionTimeOutNotification.AutoCloseDelay = 60000;

                    //set the redirect url as a value for an easier and faster extraction in on the client
                    SessionTimeOutNotification.Value = Page.ResolveClientUrl("SessionExpired.aspx");
                    MyTemplate1 objTemplate = new MyTemplate1();
                    SessionTimeOutNotification.ContentTemplate = objTemplate;
                    SessionTimeOutNotification.OnClientShowing = "notification_showing";
                    SessionTimeOutNotification.OnClientHidden = "notification_hidden";

                    SessionTimeOutNotification.CallbackUpdate += OnCallbackUpdate;

                    //SessionTimeOutNotification.CallbackUpdate += new EventHandler(OnCallbackUpdate);

                    this.Form.Controls.Add(SessionTimeOutNotification);

                    if (!ClientScript.IsStartupScriptRegistered("key1"))
                    {
                        ClientScript.RegisterStartupScript(GetType(), "key1", @"<script type=""text/javascript"" src=""../Resources/js/SessionExpired.js""></script>");
                    }

                    if (!ClientScript.IsStartupScriptRegistered("SessionControlScript"))
                    {
                        String cstext1 = "<script type=\"text/javascript\">" +
                        "serverIDs({ notificationID: 'ctl00_ContentPlaceHolder1_TestRad' });</" + "script>";
                        
                        ClientScript.RegisterStartupScript(GetType(), "SessionControlScript", cstext1);
                    }


                }
            }
            catch(Exception ex )
            {}


    
        }

        /// <summary>
        /// This is the base class for the web pages.
        /// We need to implement the common functionalities in this 
        /// class going forward.
        /// </summary>
        public class MyTemplate1 : ITemplate
        {
            protected RadButton btnMsg;
            string colnames;
            public MyTemplate1()
            {

            }
            public void InstantiateIn(System.Web.UI.Control container)
            {


                btnMsg = new RadButton();
                btnMsg.ID = "btnMsg";
                btnMsg.Text = "Continue Your Session";
                btnMsg.OnClientClicked = "ContinueSession";
                btnMsg.AutoPostBack = false;

                //boolValue.DataBinding += new EventHandler(boolValue_DataBinding);
                // boolValue.Enabled = false;
                HtmlGenericControl DivinfoIcon = new HtmlGenericControl("DIV");
                DivinfoIcon.Attributes.Add("class", "infoIcon");
                HtmlGenericControl DivinotificationContent = new HtmlGenericControl("DIV");
                DivinotificationContent.Attributes.Add("class", "notificationContent");
                HtmlGenericControl Div1 = new HtmlGenericControl("DIV");
                Label lblText = new Label();
                lblText.Text = "Time remaining: ";
                lblText.ID = "lblTime";
                HtmlGenericControl tiemlbl = new HtmlGenericControl("SPAN");
                tiemlbl.ID = "timeLbl";
                tiemlbl.InnerHtml = "60";
                tiemlbl.Attributes.Add("class", "timeLbl");
                Div1.Controls.AddAt(0, lblText);
                Div1.Controls.AddAt(1, tiemlbl);
                DivinotificationContent.Controls.AddAt(0, Div1);
                DivinotificationContent.Controls.AddAt(1, btnMsg);
                DivinfoIcon.Controls.Add(DivinotificationContent);
                container.Controls.Add(DivinfoIcon);
            }
        }

    }
}


i am getting attached error on this. Is there any better way to implement it ?

 

Please suggest is there is any better way to implement it.

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 02 May 2017, 02:41 PM

Hello Dipen,

I can suggest a few things:

  • move the form to the master page and add the controls declaratively. If the <form> is the first thing in the content palceholder, it will not make a difference in the rendered HTML whether it is in the master page or on the content page 
  • ensure that you create the notification with each postback, dynamically created controls in WebForms must be re-created by the developer each time they are needed
  • try creating the control in the Page_Init event
  • consider encapsulating this in a user control so it can have all its markup and handlers easily defined, and then just add an instance of this user control to the form
  • have the ContinueSession button invoke a different request that you know works, instead of using the callback like this (it can be a different callback handler you can implement, or a partial postback, or a web service call, depending on the project architecture)

To be honest, I am not sure if a handler can be added like that in a base class, so going with a user control may be easier.

Regards,

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Notification
Asked by
Dipen
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or