Reputation: 467
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
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
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
Reputation: 588
lbl_date.Text = DateTime.Now.ToLongDateString();
lbl_time.Text = DateTime.Now.ToLongTimeString();
Upvotes: 2