Basic Event Handling


Displaying a Web Form is a relatively simple task, but if all it did was display static text, there would be no need for ASP.NET. To make an application more usable, it needs to respond to events and perform certain actions based upon these events. The following example builds on the Hello World application. You will modify the original form to add a Button control, as shown in Figure 21.12.

Figure 21.12. Drag and drop the Button control onto the Web Form.


After this control has been added to the form, modify the Text property of the button to be "Click Me!", as shown in Figure 21.13.

Figure 21.13. The button caption is changed to Click Me!.


Next, select the Button control and add the Click event handler in the Events section of the Properties window, as shown in Figure 21.14. To add the event handler, simply double-click on the event and an event handler with a default name will be added.

Figure 21.14. An event handler is associated with the Button Click event.


TIP

You can add a default event handler to a control simply by double-clicking the control itself. For buttons, the default event handler is the Click event.


After the event handler has been added to the Button control, add the following line of code to the event handler:

 Label1.Text = "This label was changed by the Click event handler!"; 

This code will change the welcome message that was originally displayed when the button was clicked. Now that the modifications are complete, run the application. You will notice that the application looks the same as it did in the first example, with the exception of the addition of the button, as shown in Figure 21.15.

Figure 21.15. The modified Hello World application.


When the application appears, click the Click Me! button. You will notice that the displayed message changes to "This label was changed by the Click event handler!", as shown in Figure 21.16.

Figure 21.16. The modified Hello World application with the new message created in the Click event handler.


In this section, you learned the importance of event handlers and how to employ them. You can now take any web application and add the necessary event handlers to make the application useful. Listings 21.3 and 21.4 list all the code necessary to run this modified application.

Listing 21.3. WebForm1.aspx
 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="HelloWorld.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>   <HEAD>     <title>WebForm1</title>     <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">     <meta name="CODE_LANGUAGE" Content="C#">     <meta name="vs_defaultClientScript" content="JavaScript">     <meta name="vs_targetSchema" content="http://schemas.microsoft.com/                intellisense/ie5">   </HEAD>   <body MS_POSITIONING="GridLayout">     <form  method="post" runat="server">       <asp:Label        style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 40px"        runat="server">Welcome from ASP.NET Web Forms!</asp:Label>       <asp:Button          style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 88px"         runat="server"         Text="Click Me!"></asp:Button>     </form>   </body> </HTML> 

Listing 21.4. WebForm1.aspx.cs
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace HelloWorld {   /// <summary>   /// Summary description for WebForm1.   /// </summary>   public class WebForm1 : System.Web.UI.Page   {     protected System.Web.UI.WebControls.Button Button1;     protected System.Web.UI.WebControls.Label Label1;     private void Page_Load(object sender, System.EventArgs e)     {         // Put user code to initialize the page here     }     #region Web Form Designer generated code     override protected void OnInit(EventArgs e)     {         //         // CODEGEN: This call is required by the ASP.NET Web Form Designer.         //         InitializeComponent();         base.OnInit(e);     }     /// <summary>     /// Required method for Designer support - do not modify     /// the contents of this method with the code editor.     /// </summary>     private void InitializeComponent()     {       this.Button1.Click += new System.EventHandler(this.Button1_Click);       this.Load += new System.EventHandler(this.Page_Load);     }     #endregion     private void Button1_Click(object sender, System.EventArgs e)     {       Label1.Text = "This label was changed by the Click event handler!";     }   } } 



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net