FREE BOOK

Chapter 5: Event-Based Programming

Posted by Apress Free Book | ASP.NET January 02, 2009
In this chapter, we explore the intricacies of working with server control events.

Using the CustomEventTextbox Control on a Web Form

After building our new control, we are ready to put it to use in the CustomEventTextbox Web Form. This Web Form has the CustomEventTextbox control plus a button and two labels named BeforeLabel and AfterLabel that are used to track the before and after values of the control when the custom TextChanged event is raised.

Creating the event mapping in Visual Studio .NET is performed in the same manner as the previous TextChanged event in the preceding Textbox demonstration. We use the Properties window as shown in Figure 5-9 to wire up the event to the NameCustom_TextChanged handling method in the code-behind class.

The Web Form starts out with the labels displaying blank values, as shown in Figure 5-10. We enter Dale's name to cause the next form submit to raise the event. Listings 5-7 and 5-8 contain the source code for the CustomEventTextbox Web Form.



Figure 5-9. The Properties window view of our custom TextChanged event



Figure 5-10. Initial page request with the CustomEventTextbox Web Form

Listing 5-7. The CustomEventTextbox Web Form .aspx Page File

<%@ Register TagPrefix="apress" Namespace="ControlsBookLib.Ch05"
Assembly="ControlsBookLib" %>
<%@ Page language="c#" Codebehind="CustomEventTextbox.aspx.cs" AutoEventWireup="false"
Inherits="ControlsBookWeb.Ch05.CustomEventTextbox" %>
<%@ Register TagPrefix="apressUC" TagName="ControlsBookHeader"
Src="..\ControlsBookHeader.ascx" %>
<%@ Register TagPrefix="apressUC" TagName="ControlsBookFooter"
Src="..\ControlsBookFooter.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<
HEAD>
<
title>Ch05 CustomEventTextbox</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" c
ontent="http://schemas.microsoft.com/intellisense/ie5">
</
HEAD>
<
body MS_POSITIONING="FlowLayout">
<form id="Textbox" method="post" runat="server">
<apressUC:ControlsBookHeader id="Header"
runat="server" ChapterNumber="5"
ChapterTitle="Event-based Programming" />
<h3>Ch05 CustomEventTextbox</h3>
Enter your name:<br>
<apress:customeventtextbox id="NameTextbox"
runat="server"></apress:customeventtextbox><br>
<
br>
<
asp:button id="SubmitPageButton" runat="server"
Text="Submit Page"></asp:button><br>
<
br>
Before:<asp:label id="BeforeLabel" runat="server" Text=""></asp:label><br>
After:<asp:label id="AfterLabel" runat="server" Text=""></asp:label><br>
<apressUC:ControlsBookFooter id="Footer" runat="server" />
</form>
</
body>
</
HTML>

Listing 5-8. The CustomEventTextbox Web Form Code-Behind Class File

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 ControlsBookWeb.Ch05
{
    public class CustomEventTextbox : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button SubmitPageButton;
        protected System.Web.UI.WebControls.Label BeforeLabel;
        protected System.Web.UI.WebControls.Label AfterLabel;
        protected ControlsBookLib.Ch05.CustomEventTextbox NameTextbox;
        private void Page_Load(object sender, System.EventArgs e)
        {
            BeforeLabel.Text = NameTextbox.Text;
            AfterLabel.Text = NameTextbox.Text;
        }
        #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.NameTextbox.TextChanged += new
            ControlsBookLib.Ch05.TextChangedEventHandler(this.NameCustom_TextChanged);
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
        private void NameCustom_TextChanged(object o,
        ControlsBookLib.Ch05.TextChangedEventArgs tce)
        {
            BeforeLabel.Text = tce.OldValue;
            AfterLabel.Text = tce.NewValue;
        }
    }
}

We exercise the custom event by submitting the page by clicking the Submit Page button. This causes the AfterLabel control to change to "Dale Michalk," whereas the BeforeLabel keeps the old blank value, as shown in Figure 5-11.



Figure 5-11. The page after submitting the CustomEventTextbox Web Form

The Visual Studio .NET Properties window did its job in wiring up to the custom event. It was smart enough to realize we had to use TextChangedEventHandler as a delegate to wrap the NameCustom_TextChanged event-handling method. This behavior by the Designer is one more reason we recommend sticking to the event model design pattern implemented in .NET. Here is the code that performs this work:

    private void InitializeComponent()
    {
        this.NameTextbox.TextChanged += new
        ControlsBookLib.Ch05.TextChangedEventHandler(this.NameCustom_TextChanged);
        this.Load += new System.EventHandler(this.Page_Load);
    }

The following definition of NameCustom_TextChanged shows it is connected to TextChanged correctly, taking TextChangedEventArgs as its second parameter. The parameter named tce is the conduit to the information added to the BeforeLabel and AfterLabel Text values:

    private void NameCustom_TextChanged(object o,ControlsBookLib.Ch05.TextChangedEventArgs tce)
    {
        BeforeLabel.Text = tce.OldValue;
        AfterLabel.Text = tce.NewValue;
    }

Figure 5-12 shows what happens if we type a second name in the CustomEventTextbox control input box and click the Submit Page button to generate another postback. The control successfully remembers what the previous input was.



Figure 5-12. The second request with a new name on the CustomEventTextbox
Web Form

Total Pages : 12 56789

comments