在文本框中生成实时时出错 [英] Error generating live time in textbox

查看:81
本文介绍了在文本框中生成实时时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成当前日期时间的文本框,以及一个更新面板和计时器控件,可以在每一秒生成时间。但是,我的文本框在页面刷新时只生成秒数,而不是每秒。



我尝试了什么:



这是计时器的代码:



< asp:UpdatePanel ID =UpdatePanel1runat =server>

< contenttemplate>

< asp:Timer ID =Timer1runat =serverOnTick =Timer1_TickEnabled =true>





代码落后:



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{



txtTransDate.Text = DateTime.Now.ToString(yyyy-MM-dd hh:mm:ss);

}



}



protected void Timer1_Tick(object sender,EventArgs e)

{

txtTransDate.Text = D. ateTime.Now.ToString(yyyy-MM-dd hh:mm:ss);

}

I have a textbox that generates current datetime and a updatepanel and timer control to generate time at each seconds.However, my textbox is generating seconds at page refreshment only not at each seconds.

What I have tried:

This is the code for timer:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<contenttemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Enabled="true">


code behind:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

txtTransDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}

}

protected void Timer1_Tick(object sender, EventArgs e)
{
txtTransDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}

推荐答案

不要使用服务器显示当前日期或时间:使用客户端:Javascript。

如果您编写C#代码来更新时间,那么每秒需要一个回发,往返服务器通过互联网,一些Ajax,以防止刷新整个页面等。

使用Javascript,它都保留在客户端浏览器中: W3Schools显示时钟 - Tryit Editor v3.1 [ ^ ]
Don't use the server to display the current date or time: use the client: Javascript.
If you write C# code to update the the time, then every second you need a postback, a round trip to the server via the internet, some Ajax to prevent refreshing the whole page, etc.
Use Javascript and it all stays in the client browser: W3Schools "Display a clock" - Tryit Editor v3.1[^]


这篇关于在文本框中生成实时时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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