Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anyone let me know what is wrong with this code? I switched it from C# to vb and it works without any mistakes in c# i want to place timer in web form and not reset with postback or click button

default.aspx

ASP.NET
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <style type="text/css">
#CountDownPanel {color: blue; background-color: yellow; font-size: 18px;}
    </style>
    <asp:Label ID="Label1" runat="server" Text="Time Remaining:"></asp:Label>&nbsp;
        <span id="CountDownPanel" runat="server"></span>
        <asp:HiddenField ID="TotalSeconds" runat="server" Value="2700" />
    </div>
    </form>
</body>
</html>


default.aspx.vb

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            NoCache(HttpContext.Current)
        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        If Page.IsPostBack Then
            Dim ticks As Long = Convert.ToInt64(Me.StartTicks)
            Dim startTime As DateTime = New DateTime(ticks)
            Dim elapsed As TimeSpan = (startTime.AddMinutes(180) - DateTime.Now)
            Me.TotalSeconds.Value = Convert.ToInt32(elapsed.TotalSeconds).ToString()
        End If

        ClientScript.RegisterClientScriptInclude("script", "CountDown.js")
    End Sub

    Private ReadOnly Property StartTicks As String
        Get
            Dim startTicksString As String = CType(Session("StartTicks"), String)
            If String.IsNullOrEmpty(startTicksString) Then
                startTicksString = DateTime.Now.Ticks.ToString()
                Session("StartTicks") = startTicksString
            End If

            Return startTicksString
        End Get
    End Property

    Private Sub NoCache(ByVal context As HttpContext)
        context.Response.AddHeader("Pragma", "no-cache")
        context.Response.CacheControl = "no-cache"
        context.Response.Cache.SetAllowResponseInBrowserHistory(False)
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        context.Response.Cache.SetNoStore()
        context.Response.Expires = -1
    End Sub
End Class


countdown.js

var _countDowncontainer = 0;
var _currentSeconds = 0;

function ActivateCountDown(strContainerID, initialValue) {
    _countDowncontainer = document.getElementById(strContainerID);

    if (!_countDowncontainer) {
        alert("count down error: container does not exist: " + strContainerID +
			"\nmake sure html element with this ID exists");
        return;
    }

    SetCountdownText(initialValue);
    window.setTimeout("CountDownTick()", 1000);
}

function CountDownTick() {
    if (_currentSeconds <= 0) {
        alert("your time has expired!");
        //document.getElementById("ctl01").disabled = true;
        document.getElementById("ctl01").click();
        return;
    }

    SetCountdownText(_currentSeconds - 1);
    window.setTimeout("CountDownTick()", 1000);
}

function SetCountdownText(seconds) {
    //store:
    _currentSeconds = seconds;

    //get minutes:
    var minutes = parseInt(seconds / 60);

    //shrink:
    seconds = (seconds % 60);

    //get hours:
    var hours = parseInt(minutes / 60);

    //shrink:
    minutes = (minutes % 60);

    //build text:
    var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);

    //apply:
    _countDowncontainer.innerHTML = strText;
}

function AddZero(num) {
    return ((num >= 0) && (num < 10)) ? "0" + num : num + "";
}
window.onload = WindowLoad;

function WindowLoad(event) {
    ActivateCountDown("CountDownPanel", document.getElementById("TotalSeconds").value);
}


What I have tried:

I TRY THIS CODE with VB.NET but NOT WORKING. I CONVERTed IT FROM C#. It works WITH C#
Posted
Updated 14-May-17 14:16pm
v2

1 solution

You didn't indicate what not working in the post. Look like the Page_PreRender did not get trigger. Couple of thing you do. I think #1 should do the trick based on your scenario.

1. Change the AutoEventWireup="false" to AutoEventWireup="true"

2. Leave the AutoEventWireup="false" and change the method
VB
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
End Sub

TO
VB
Protected Overrides Sub OnPreRender(e As EventArgs)
        MyBase.OnPreRender(e)
 End Sub

asp.net - OnPreRender vs Page_PreRender - Stack Overflow[^]
 
Share this answer
 
Comments
ahmed_hashem 15-May-17 0:58am    
thank you it working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900