Thursday 16 March 2017

Show Alert before session expire in asp.net web application using c#.


<style type="text/css">
        .modalBackground {
            background-color: Black;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }

        .modalPopup {
            background-color: #FFFFFF;
            width: 300px;
            border: 3px solid #006990;
            border-radius: 12px;
            padding: 0;
        }

            .modalPopup .header {
                background-color: #006990;
                height: 30px;
                color: White;
                line-height: 30px;
                text-align: center;
                font-weight: bold;
                border-top-left-radius: 6px;
                border-top-right-radius: 6px;
            }

            .modalPopup .body {
                padding: 10px;
                min-height: 50px;
                text-align: center;
                font-weight: bold;
            }

            .modalPopup .btnSection {
                padding: 6px;
            }

            .modalPopup .yes, .modalPopup .no {
                height: 23px;
                color: White;
                line-height: 23px;
                text-align: center;
                font-weight: bold;
                cursor: pointer;
                border-radius: 4px;
            }

            .modalPopup .yes {
                background-color: #006990;
                border: 1px solid #006990;
            }

            .modalPopup .no {
                background-color: #9F9F9F;
                border: 1px solid #5C5C5C;
            }
    </style>

 <asp:LinkButton ID="lnkFake" runat="server" />
                        <asp:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout" runat="server" PopupControlID="pnlPopup"
                            TargetControlID="lnkFake" OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground"
                            OnOkScript="ResetSession()">
                        </asp:ModalPopupExtender>
                        <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
                            <div class="header">
                                Session Expiring!
                            </div>
                            <div class="body">
                                Your Session will expire in&nbsp;<span id="seconds"></span>&nbsp;seconds.<br />
                                Do you want to reset?
                            </div>
                            <div class="btnSection" align="right">
                                <asp:Button ID="btnYes" runat="server" Text="OK" CssClass="yes" />
                               <%-- <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" />--%>
                            </div>
                        </asp:Panel>
                        <script type="text/javascript">
                            function SessionExpireAlert(timeout) {
                                var seconds = timeout / 1000;
                               // document.getElementsByName("secondsIdle").innerHTML = seconds;
                                document.getElementsByName("seconds").innerHTML = seconds;
                                setInterval(function () {
                                    seconds--;
                                    document.getElementById("seconds").innerHTML = seconds;
                                 //   document.getElementById("secondsIdle").innerHTML = seconds;
                                }, 1000);
                                setTimeout(function () {
                                    //Show Popup before 20 seconds of timeout.
                                    $find("mpeTimeout").show();
                                }, timeout - 60 * 1000);
                                setTimeout(function () {
                                    //alert(" Your Session has expired.");
                                    window.location = window.location.href;
                                   // window.location = "Expired.aspx";
                                }, timeout);
                            };
                            function ResetSession() {
                                //Redirect to refresh Session.
                                //window.location = window.location.href;
                                $.ajax({
                                    type: "POST",
                                    contentType: "application/json; charset=utf-8",
                                    url: '<%=ResolveUrl("~/area/login.aspx/ResetSession") %>',
                                    dataType: "json",
                                    success: function (data) {
                                        SessionExpireAlert(data.d);
                                    },
                                    error: function (result) {
                                        alert("Error");
                                    }
                                });
                            }
                        </script>

 protected void Page_Load(object sender, EventArgs e)
    {
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
                if (!Page.IsPostBack)
                {
                    Session["Reset"] = true;
                    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
                    SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
                    int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
                    ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
                }
}

Use of Web Method to avoid reload page and maintain  page data

[WebMethod]
    public static string ResetSession()
    {
        int TimeOut = 0;
        try
        {
            HttpContext.Current.Session["Reset"] = true;
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
            SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
            TimeOut = (int)section.Timeout.TotalMinutes * 1000 * 60;
            //ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
        }
        catch (Exception ex)
        {

        }
        return TimeOut.ToString();
    }


IN WEB CONFIG-

 <sessionState timeout="2"/>

No comments:

Post a Comment