[英]Set an asp:HiddenField value from codebehind and access in javascript function
I'm trying to setup a countdown timer for an asp:Timer
which will refresh the page upon finishing. 我正在尝试为asp:Timer
设置倒数计时器,该asp:Timer
将在完成后刷新页面。 What I'm doing is setting the timer interval and a hidden value when it initializes, and then calling a javascript function which starts the countdown timer using the hiddenfield's information. 我正在做的是设置计时器间隔和初始化时的隐藏值,然后调用一个JavaScript函数,该函数使用hiddenfield的信息启动倒数计时器。
The interval gets set, and the hidden field value gets set, but after trying various things, the results I get are a problem where the hiddenfield value is uninitialized when the javascript runs. 设置间隔,设置隐藏字段值,但是尝试各种方法后,得到的结果是一个问题,其中当javascript运行时,隐藏字段值未初始化。 It's uninitialized when I try to call the function directly on document load as well. 当我尝试直接在文档加载时调用该函数时,它也未初始化。
Here is the code I'm currently using. 这是我当前正在使用的代码。
codebehind 代码隐藏
DateTime refreshTime;
protected void refreshHidden_Init(object sender, EventArgs e)
{
refreshTime = DateTime.Now.AddSeconds(60);
refreshHidden.Value = refreshTime.ToString();
int timeToFresh = (int)(refreshTime - DateTime.Now).TotalMilliseconds;
refreshTimer.Interval = timeToFresh;
refreshTimerJS();
}
protected void refreshTimerJS()
{
StringBuilder script = new StringBuilder();
script.Append("<script type=\"text/javascript\">");
script.Append("var dt='");
script.Append(refreshTime.ToString());
script.Append("';");
script.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DateVars", script.ToString());
}
the page 这一页
<div id="autopriseUpdate" class="content">
<asp:Timer runat="server" ID="refreshTimer" Interval="600000" OnTick="refreshTimer_Tick"></asp:Timer>
<asp:Label runat="server" ID="refreshLabel" Text="1m 0s"></asp:Label>
<asp:HiddenField runat="server" ID="refreshHidden" OnInit="refreshHidden_Init"/>
</div>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery.countdown.min.js"></script>
<script type="text/javascript" src="js/refresh.js"></script>
javascript javascript
$(document).ready(function () {
refreshJS();
});
function refreshJS(){
var dt = new Date($('#refreshHidden').value);
$('#refreshLabel').countdown(dt).on('update.countdown', function (event)
{
$(this).html(event.strftime('%M:%S'));
});
}
The hiddenfield value is set and the asp:Timer interval counts down and refreshes right on the dot, but the countdown timer doesn't start up. 设置了hiddenfield值,并且asp:Timer间隔倒计时并在点上刷新,但是倒数计时器没有启动。 My best guess is that there's something I need to fix in the order things happen, but I haven't got a clue. 我最好的猜测是,我需要按照发生的顺序修复某些问题,但是我没有头绪。
I can't just hardcode the countdown value because I'm going to make the refreshTime variable, so the user can choose how many minutes are between page refreshes. 我不能仅对倒计时值进行硬编码,因为我将要设置refreshTime变量,因此用户可以选择两次页面刷新之间的间隔时间。
If anybody has an idea for how to get my javascript working or how to get the DateTime value from codebehind some other way and utilize it, I would really appreciate the help! 如果有人对如何使我的JavaScript正常工作或如何从代码背后以其他方式获取DateTime值并加以利用的想法,我将非常感谢您的帮助!
As an alternative to Mike's answer you could also set ClientIDMode to static and your jQuery will work as is. 作为Mike答案的替代方法,您还可以将ClientIDMode设置为static,并且jQuery将照常运行 。 (Assuming you're using .NET 4.0 or above) (假设您使用的是.NET 4.0或更高版本)
<asp:HiddenField runat="server" ClientIDMode="Static" ID="refreshHidden" OnInit="refreshHidden_Init"/>
Nope ! 不 !
Your JavaScript can't access the value using: 您的JavaScript无法使用以下方式访问值:
var dt = new Date($('#refreshHidden').value);
It's called refreshHidden
in the ASP.Net world, but when the page is rendered, it'll have a different name. 在ASP.Net世界中,它称为refreshHidden
,但是在呈现页面时,它将具有不同的名称。
Here's what your code should look like: 这是您的代码应如下所示:
var hiddenField = $("#<%= refreshHidden.ClientID %>").val();
var dt = new Date(hiddenField);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.