Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
}
Posted
Updated 8-Nov-16 19:59pm

1 solution

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[^]
 
Share this answer
 

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