Microsoft AJAX Timer + Cookies Timeout Checking
test.aspx (Front Page) |
………………………………… <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="Button1" runat="server" Text="Test" /><BR /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label><BR /> <asp:Timer ID="Timer2" runat="server" OnTick="ABC"></asp:Timer> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label4" runat="server" Text="" Visible="false"></asp:Label> </ContentTemplate> </asp:UpdatePanel> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> ………………………………… |
test.aspx.vb (Code Behind) |
Public Partial Class WebForm1 Inherits System.Web.UI.Page Dim Timeout As Integer = 7 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Timer2.Interval = "1000" End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click If ScriptManager1.IsInAsyncPostBack Then Dim myCookie As New HttpCookie("Test") myCookie.Value = "Test" myCookie.Expires = DateTime.Now.AddSeconds(Timeout) Response.Cookies.Add(myCookie) If Not Request.Cookies("Test") Is Nothing Then Label1.Text = "Cookies Enabled : Yes. " Label2.Text = "Cookies Timeout in " & Timeout.ToString & " Second(s)" Label4.Text = "" End If End If End Sub Protected Sub ABC(ByVal sender As Object, ByVal e As EventArgs) Handles Timer2.Tick If ScriptManager1.IsInAsyncPostBack Then If Not Request.Cookies("Test") Is Nothing Then If Label4.Text = "" Then Label4.Text = (Timeout – 1).ToString Label2.Text = "Cookies Timeout in " & Label4.Text & " Second(s)" Else Label4.Text = (CInt(Label4.Text) – 1).ToString Label2.Text = "Cookies Timeout in " & Label4.Text & " Second(s)" End If Else Label1.Text = "Cookies Enabled : No. " Label4.Text = "" Label2.Text = "" End If End If End Sub End Class |