开发者

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 page开发者_Go百科s?


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 :)


Use Ajax Extension controls (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜