Adding a Page Load Event Handler

A Web form has many different types of events that can be trapped and handled in your code-behinds.  Each control has its own events, and so does the page itself.  Every time a page is loaded, for example, a page load event is fired.  You will add code to the page load event handler to update the current date/time Label control you added in the previous lesson.

1.  Double-click on the design surface to have VWD automatically add a Page_Load event handler to the page's code-behind class and load the code-behind page.



VB

Notice that this class has been declared as a "partial class".  Partial classes are new in the .NET Framework 2.0.  A partial class allows you to use multiple source files for the same class.

2. To the handler add the following line of code, which displays the current date and time in the Label control whenever this page loads.


dateTimeLabel.Text = DateTime.Now.ToString
VB

3.  In the Solution Explorer ensure that the Authors.aspx page is selected. A nice new feature of VWD is that you no longer have to right-click and select Set As Start Page. Instead, the currently selected page loads automatically when you run the site. Run the Web application by pressing F5, by selecting the Debug | Start menu command, or by clicking the Start Debugging icon, as you see below.



VB

4.  This is the first time the Web site is being run so you will be prompted on how to handle debugging.  For this tour you want to run with debugging enabled, so for VB select Modify the Web.config file to enable debugging and for C# select Add a new Web.config file with debugging enabled. and click on OK.  This will add/modify a web.config file in your site.

VB

C#

VWD has a built-in Web server that automatically starts when you run your Web site.  The Web server is secure in that it can only be accessed from the local machine and the port it runs on is randomly selected.  You can control the Web server by right clicking on the Web server icon that appears within your system tray.

The Authors.aspx page is launched.  You can refresh the Web page and see the current date/time being displayed as the Page_Load event is fired every time the page is loaded.

5.  Close the browser.

 
HyperLink HyperLink

Powered By ASP.NET v2.0