Setting control properties using attributes Adding event handlers

250 52. asp:Button id=btnBlue runat=server Text=Blue 53. asp:Button id=btnBlack runat=server Text=Black 54. form 55. body html Here, the code-behind .vb file is referenced using the Src attribute; in the Visual Studio .NET code shown earlier, the code-behind file was referenced in the CodeBehind attribute. This is an important distinction. The CodeBehind attribute is used only by Visual Studio .NET; its ignored by the ASP.NET compiler. Be sure that the value of the id attribute of each asp:... tag matches the name of the associated member variable in the code-behind class. This name is how the ASP.NET framework matches up each member variable with the corresponding server control. To run this application, enter the two code fragments and save them into two files, named HelloBrowser.vb and HelloBrowser.aspx, respectively. The files must be located in an IIS virtual directory on a machine with the .NET Framework installed. After you save the files, point a web browser to HelloBrowser.aspx. The ASP.NET runtime automatically compiles and runs the application, delivering the output to the browser.

6.1.2.1 Setting control properties using attributes

The properties of server control objects can be initialized by specifying values for attributes within the asp:... tags. For example, the button created by the following tag has its Text property set to Green : asp:Button id=btnGreen runat=server Text=Green This assignment could instead have been handled elsewhere in code—either in a script block or in a code-behind class. In general, any writable property can be set using an attribute of the same name.

6.1.2.2 Adding event handlers

Define event handlers directly in the code-behind class for any events you wish to handle. For example: Private Sub btnBlack_Click _ ByVal sender As Object, _ ByVal e As System.EventArgs _ Handles btnBlack.Click Label1.ForeColor = System.Drawing.Color.Black End Sub Private Sub btnBlue_Click _ ByVal sender As System.Object, _ ByVal e As System.EventArgs _ Handles btnBlue.Click Label1.ForeColor = System.Drawing.Color.Blue End Sub Private Sub btnGreen_Click _ ByVal sender As System.Object, _ ByVal e As System.EventArgs _ Handles btnGreen.Click Label1.ForeColor = System.Drawing.Color.Green 251 End Sub

6.2 Handling Page Events

The base Page class may at times raise events. These events can be handled by the derived Page class the code-behind class or by code embedded in the web page. Although its possible to define an event-handler subroutine, the preferred response to events raised by the Page class is to override the protected methods provided by the Page class. For example, the following method could be placed in the code-behind class, providing a way to respond to the loading of the page: Protected Overrides Sub OnLoade As EventArgs MyBase.OnLoade ... End Sub When overriding an On Event -style method, ensure that the overriding method calls the base-class implementation, in addition to its own processing. The job of the base-class implementation is to raise the corresponding event. If the base-class implementation is not called, the event wont be raised. Following is the list of events a Page object might raise. The Page class itself doesnt define these events: it inherits them from its parent classes. This list of events includes a brief description of each event, the class in which the event is defined, and the syntax for overriding the protected method that corresponds to the event. AbortTransaction inherited from System.Web.UI.TemplateControl Raised when a COM+ transaction in which the page is participating is aborted. Its syntax is: Protected Overrides Sub OnAbortTransactionByVal e As EventArgs CommitTransaction inherited from System.Web.UI.TemplateControl Raised when a COM+ transaction in which the page is participating is committed. Its syntax is: Protected Overrides Sub OnCommitTransactionByVal e As EventArgs DataBinding inherited from System.Web.UI.Control Raised when the page binds to a data source. Its syntax is: Protected Overrides Sub OnDataBindingByVal e As EventArgs Disposed inherited from System.Web.UI.Control Raised after the Page objects Dispose method has finished its processing. The Dispose method is called by the ASP.NET framework after the page has been rendered and the object is no longer needed. There is no corresponding OnDisposed method. To add a handler for this event in a derived class, use the following code: Private Sub Page_Disposed _ ByVal sender As Object, _ ByVal e As EventArgs _ Handles MyBase.Disposed ... End Sub Error inherited from System.Web.UI.TemplateControl