倒数计时器避免回发ASP.NET VB [英] Countdown timer avoiding postback ASP.NET VB

查看:100
本文介绍了倒数计时器避免回发ASP.NET VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁能让我知道这段代码有什么问题?我将它从C#切换到vb并且它在c#中没有任何错误工作我希望将计时器放在网络表单中而不是使用回发或单击按钮重置



default.aspx



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

<%@ 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





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





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);
}





我的尝试:



我用VB.NET尝试这个代码但不工作。我从C#转发了它。它适用于C#



What I have tried:

I TRY THIS CODE with VB.NET but NOT WORKING. I CONVERTed IT FROM C#. It works WITH C#

推荐答案

你没有说明帖子中有什么不起作用。看起来Page_PreRender没有触发器。你做的几件事。我认为#1应该根据你的场景做到这一点。



1.将AutoEventWireup =false更改为AutoEventWireup =true



2.保留AutoEventWireup =false并更改方法

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
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
End Sub



to


TO

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



asp.net - OnPreRender与Page_PreRender - Stack Overflow [ ^ ]


这篇关于倒数计时器避免回发ASP.NET VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆