Solved

How do I Auto update a label with the current date when a page loads (ASP.Net VB)?

Posted on 2009-03-31
6
477 Views
Last Modified: 2013-11-07
I have created a label on an ASP.Net page that I want to show the current date & time every time the page is loaded/refreshed., but Im a little rusty having not programmed for more than four years.  I was trying to use the NOW() function but cant remember the syntax for creating a page load event.
0
Comment
Question by:kieranlarkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Expert Comment

by:Causality
ID: 24027425
put a Label on your Form.
then you can write this code on FormOnLoad event


        private void Form1_Load(object sender, EventArgs e)
        {
             label1.Text=DateTime.ToString("MM.dd.yyyy HH:mm:ss");
         }
0
 
LVL 2

Expert Comment

by:Causality
ID: 24027431
Sorry I forget Now property,

This is The Correct Code :

     private void Form1_Load(object sender, EventArgs e)
        {
             label1.Text=DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss");
         }
0
 
LVL 3

Expert Comment

by:dipakdave1983
ID: 24028171
if u want to gt date of client i.e indian user can see date like GMT+530.  then render date in javascrilt.

 at the end of html markup of your page  do like.

 document.getElementById('<%# label1.ClientID %>').value=getdate();    

NOTE: // getDate() is what I  mean by javascript function i dont have it hendy.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:kieranlarkin
ID: 24028276
Thanks Guys but I was looking for a solution in VB for ASP.Net....if you can help me with that I'd appreciate it.
0
 
LVL 3

Expert Comment

by:dipakdave1983
ID: 24028915
what i have suggested doesnt matter about language. thats in markup so no language
0
 

Accepted Solution

by:
kieranlarkin earned 0 total points
ID: 24030612
In your VB codefile:

Select the Page Events Class THEN select the LoadComplete event handler

Then by using the Format() and Now() functions within this section and assinging it to the text element of your label you will display the current date and time in whatever datetime format you need as in the example below:
    Private Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        MyVariable.Text = Format(Now(),"HH:mm")
    End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question