Introduction

In this tutorial we will learn how to display the date and time on a web form using the label control. This tutorial gives you a quick reference to writing the code for the click event of a button control to display the date and time. The code presented in this tutorial is in C#.

Displaying the date and time using a label control

1. Open Visual Studio 2010 or Visual Studio 2010 Web Developer Express and create a new web form and label it Containers. Make sure the Place Code in Separate File box is checked.

2. Switch to design view.

3. In the toolbox under the standard menu, click and drag a button control onto the page.

4. In the properties panel, type the text “Date and Time” into the text property.

5. In the toolbox under the standard menu, Drag a label control onto the page in design view and make sure it is to the right of the button control. Name it “Label1″(Default Name)

6. In the properties panel, delete the text property of the label control and leave it blank.

7. Double click the button control to open the code behind page.

8. Copy and paste the following code into the click event for the button control.

10. Save the project, then run or debug your application.

Click on the button in the browser to display the date and time.

Conclusion

In this Tutorial we learned how to Display the Time and Date to a Web Form. This can be used in many different instances to put time stamps on pages, to display a clock on your site, or to keep track of the last time you clicked a button or triggered an event.