locked
I've Countdown Timer. But, If REFRESH PAGE - The timer is initial. RRS feed

  • Question

  • User-557095619 posted

    This is my code

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="twoUpdatePanel.aspx.cs" Inherits="twoUpdatePanel" %>
    
    <!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>2 update panel</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:ScriptManager ID="SM1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
            </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            EXAM 1<br /><br />
                    <asp:Label ID="lblTimer" runat="server"></asp:Label>
                    <br />
                    <br />
                    
            </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
        <br /><br />
            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    Favourite Actor:
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>--Select--</asp:ListItem>
                        <asp:ListItem>Mel Gibson</asp:ListItem>
                        <asp:ListItem>Liam Neeson</asp:ListItem>
                        <asp:ListItem>Eric Bana</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <br />
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
                    <asp:HiddenField ID="HiddenField1" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        
        </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class twoUpdatePanel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!SM1.IsInAsyncPostBack)
            {
                Session["timeout"] = DateTime.Now.AddHours(Convert.ToDouble(1)).AddMinutes(Convert.ToDouble(0)).AddSeconds(Convert.ToDouble(0)).ToString();
            }
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString())))
            {
                lblTimer.Text = string.Format("Time Left: 00:{0}:{1}", ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString());
            }
            else
            {
                timer1.Enabled = true;
                Response.Redirect("Logout.aspx");
    
            }
        }
    }


    My question

    1- If user refresh page, the page force to Logout.aspx. How to do that?

    or

    2- If user refresh page, my Countdown Timer NOT INITIAL. But just continue to as usual.

    Please help. I'm stuck...

    Monday, March 17, 2014 8:47 AM

Answers

All replies

  • User-821857111 posted

    Cookies are a solution for both scenarios. 

    1- If user refresh page, the page force to Logout.aspx. How to do that?

    Use Javascript to check to see if a cookie exists containing the name of the page. If it does, redirect to Logout.aspx. If not, write the name of the page to a cookie.

    2- If user refresh page, my Countdown Timer NOT INITIAL. But just continue to as usual.

    Use Javascript to keep a record of the time as it's being counted down in a cookie. When the page is requested, check to see if a cookie exists with a time. If it does, start the countdown with whatever value was stored.

    Monday, March 17, 2014 10:11 AM
  • User-557095619 posted

    I never used Cookies before. Really no idea how to start the coding Cry

    Monday, March 17, 2014 2:31 PM
  • User-821857111 posted

    There are loads of tutorials that show how to get and set cookies from javascript

    https://www.google.co.m/search?q=get+and+set+cookies+javascript

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 17, 2014 3:18 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 17, 2014 11:47 PM