Carisle
Carisle

Reputation: 467

Timer Tick in Asp.net?

whats is the equivalent of these in asp? it doesn't work for me!

lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)

Upvotes: 0

Views: 3577

Answers (3)

techBeginner
techBeginner

Reputation: 3850

There is no Timer control in ASP.NET as such, but AJAX timer.

In that case you have to put these labels inside AJAX update panel as follows

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick">
        </asp:Timer>
        <asp:Label ID="lbl_date" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="lbl_time" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

and in code behind for Timer1_Tick event put

lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)

this should work..

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

The thing to remember here is that all your vb code does is generate an html document, and nothing else. Once that job is done, the page class you're working in is even sent off to the garbage collector and the processor thread you were in is repurposed to serve another http request. Therefore trying to set a label based on a timer event is just plain nuts — your timer will likely be disposed before it ever has a chance to tick.

Instead, you want to do this particular job in javascript. Look into javascript's setTimeout() method.

Upvotes: 1

Sean Barlow
Sean Barlow

Reputation: 588

lbl_date.Text = DateTime.Now.ToLongDateString();
lbl_time.Text = DateTime.Now.ToLongTimeString();

Upvotes: 2

Related Questions