Article

Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Next

How It Works

In this example, a CurrentTimeButton server control is declared, and its corresponding Text property is set to "Click for current time...":

<asp:Button id="CurrentTimeButton" runat="server"    
Text="Click for current time..." OnClick="UpdateTime" />

Additionally, we've assigned the name of our custom event handler, UpdateTime, to the OnClick event method for this button control.

The OnClick event method for the Button control is essentially a placeholder, which can be assigned the name of a method (which we write, and in this case is UpdateTime) to perform the processing when that particular event is raised. The method name we assign to the OnClick handler must conform to the required "method syntax" (same number or arguments, same types, same return value) defined by the event method.

We then defined our custom UpdateTime event handler to update the button text with the current time as follows:

Public Sub UpdateTime (ByVal sender As Object, _    
    ByVal e As system.EventArgs)    
 ' Perform custom logic here -- update the button text with      
   current time        
 CurrentTimeButton.Text = DateTime.Now.ToShortTimeString()    
End Sub

All we are doing here is firstly establishing the UpdateTime subroutine, and then assigning to the Text property of our CurrentTimeButton, the values DateTime.Now.ToShortTimeString(). This is basically saying, "when the UpdateTime sub routine is triggered, display the current time in the text property of CurrentTimeButton, which is a string data type".

Page Lifecycle

Although this topic is covered elsewhere in this book, it's worthwhile to review the System.Web.UI.Page object's lifecycle, in the context of ASP.NET server controls. Specifically, we will briefly review how the ASP.NET Page class loads, processes events, and closes down. When designing a web form, you are really referencing the base functionality of the ASP.NET Page class. As such, the Page class offers its own methods, properties, and events to the form. When loading a web form for the first time, you might, for example, want to preload the Page object's server controls with values from a database, or set property values to various server controls on the page dynamically. The following listings provide an overview of the various methods that are commonly overridden in your ASPX Page object implementation, that allow you to perform processing during the various stages of the Page object's lifetime.

Page_Load

The Page_Load method is a virtual method (recall that a virtual method of an object is one which you can override) of the Page class, which means it can be (and often is) overridden in the Page class implementation. The Page_Load method is invoked when the ASPX page is loaded for the first time, or refreshed. The following is an example implementation of the Page_Load method:

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)    
If Not Page.IsPostback Then    
 ' First time page loads --    
 ' perform initialization here!      
End If      
End Sub

The most interesting part of the above listing is the reference to the Page class's IsPostback property. The IsPostback property is significant because this property can be used to distinguish whether or not a page is being loaded for the very first time, or if it's being loaded as the result of what is commonly referred to as a "Postback" -- in other words, if a Button server control was clicked, an OnClick event would be raised and the form data would be posted back to the server -- hence the term Postback. We have seen this method several times in the past few chapters.

The most common uses for implementing the Page_Load method in your ASPX pages are to:

  • check whether this is the first time the page is being processed, or to perform processing after being refreshed.
  • perform data binding the first time the page is processed, or re-evaluate data binding expressions on subsequent round trips, to display the data sorted differently, for example.
  • Read and update control properties.

Event Handling

The second part of a page's lifecycle, is the event handling stage. After an ASPX page is loaded and displayed, additional event handlers will be invoked when control events are raised. For example, after a user clicks an ASP.NET Button control, the OnClick event will be raised, thus posting the event to the server. If an event handler is written and assigned to process the OnClick event for that particular control, it will be invoked whenever the Button control is clicked.

Not all controls perform this type of automatic "posting back" to the server when an event is raised. For example, the TextBox control does not, by default, post back notification to the server when its text changes. Similarly, the ListBox and CheckBox server controls do not, by default, post back event notifications to the server every time their selection state changes. For these particular controls, their AutoPostBack property (which can be set to either True or False) would need to explicitly be set to True in the control's declaration (or set programmatically within the code) to enable automatic post back of event/state changes to the server for processing.

If you create an ASP.NET server control that performs server-side processing whenever the control's state changes (like when a CheckBox is checked), and you don't seem to be getting the results you expect, check if the control has an AutoPostBack property, and if so, set it to True. This property typically defaults to False if not explicitly declared when the control was defined. We'll take a closer look at the AutoPostBack property in action in the Try It Out section that follows.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links