简体   繁体   中英

c# update current time,days,date

protected void Page_Load(object sender, EventArgs e)
{
    todayDate.Text=DateTime.Now.toString();
}

Output

10:28AM 9/28/2011

the current time is never update, the result is static, how do i set a interval in c# in order to update current time?

updated information

this is my ajax updated timer code

  <ajax:ToolkitScriptManager ID="scriptmanager1" runat="server">

    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />

and i called it by using

protected void UpdateTimer_Tick(object sender, EventArgs e)
{
    DateStampLabel.Text = DateTime.Now.ToString() + "  "+DateTime.Today.DayOfWeek.ToString(); 


}

output is exactly what i want, but the effect not exactly what i want, cause when user clicked on some button,the label will blink, this like telling the user the page is reloading, so i wondering is that any idea can deliver update time from server? or just refresh partial pages?

the reason it doesn't update is because the ASP.Net is server code that is generating HTML which is presented in the browser.

The browser sends a request to your server, the server executes your page logic on one of it's threads, your code generates output (which in your case, setting the today.Data.Text is creating html in the output buffer), the response is sent to the client and the browser shows that html. You're done. The request was fulfilled.

You either need to keep refreshing that page (bad - old full refresh server generated page model) or have javascript code that runs in the browser. Checkout Ajax patterns for that.

Hope that gives insight as to why :)

使用Ajax Extension控件( UpdatePanel,Timer )。

As has been suggested you need to use a client-side script to update the values in realtime, if this is your intention. Searching for 'JavaScript clock' or 'Ajax clock' should give you some cut-n-paste code, or look into some Ajax examples in their toolkit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM