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 Pager Control on a Web Form
 
The Pager Event Bubbling Web Form demonstrates the Pager control in both its button and hyperlink display motifs. A single label represents the PageCommand activity generated by the two controls. The first request for the page appears in the browser, as shown in Figure 5-18. Listings 5 17 and 5-18 provide the .aspx and code-behind files for this Web Form.
 
 
 
 Figure 5-18. The Pager Event Bubbling Web Form rendering its first request

 Listing 5-17. The Pager Event Bubbling Web Form .aspx Page File
 
 
<%@ Register TagPrefix="apress" Namespace="ControlsBookLib.Ch05"
 
Assembly="ControlsBookLib" %>
 <%@ Page language="c#" Codebehind="PagerEventBubbling.aspx.cs"
 
AutoEventWireup="false"
 
Inherits="ControlsBookWeb.Ch05.PagerEventBubbling" %>
 <%@ 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 Pager Event Bubbling</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"
 
content="http://schemas.microsoft.com/intellisense/ie5">
 </
HEAD>
 <
body MS_POSITIONING="FlowLayout">
 
<form id="PagerEventBubbling" method="post" runat="server">
 
<apressUC:ControlsBookHeader id="Header" runat="server" ChapterNumber="5"
 
ChapterTitle="Event-based Programming" />
 
<h3>Ch05 Pager Event Bubbling</h3>
 
<apress:pager id="pager1" display="button" runat="server"></apress:pager><br>
 
<br>
 <
h3>Direction:&nbsp;<asp:Label ID="DirectionLabel"
 
Runat="server"></asp:Label></h3>
 <
apress:pager id="pager2" display="hyperlink"
 
runat="server"></apress:pager><br>
 <
br>
 <
apressUC:ControlsBookFooter id="Footer" runat="server" />
 
</form>
 </
body>
 </
HTML>
 
 
Listing 5-18. The Pager Event Bubbling 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 PagerEventBubbling : System.Web.UI.Page
 
    {
         protected System.Web.UI.WebControls.Label DirectionLabel;
         protected ControlsBookLib.Ch05.Pager pager1;
         protected ControlsBookLib.Ch05.Pager pager2;
         private void Page_Load(object sender, System.EventArgs e)
         {
             DirectionLabel.Text = "<none>";
         }
 
        #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.pager1.PageCommand +=
             new ControlsBookLib.Ch05.PageCommandEventHandler(this.Pagers_PageCommand);
             this.pager2.PageCommand +=
             new ControlsBookLib.Ch05.PageCommandEventHandler(this.Pagers_PageCommand);
             this.Load += new System.EventHandler(this.Page_Load);
         }
 
        #endregion
 
        private void Pagers_PageCommand(object o,
         ControlsBookLib.Ch05.PageCommandEventArgs pce)
         {
             DirectionLabel.Text =
             Enum.GetName(typeof(ControlsBookLib.Ch05.PageDirection),
             pce.Direction);
         }
     }
 }

 
The Pager controls are wired to the same event handler in the code-behind class named Pagers_PageCommand in the InitializeComponent method of the Web Form:
 
 
private void InitializeComponent()
 {
 
this.pager1.PageCommand += new
 
ControlsBookLib.Ch05.PageCommandEventHandler(this.Pagers_PageCommand);
 
this.pager2.PageCommand += new
 
ControlsBookLib.Ch05.PageCommandEventHandler(this.Pagers_PageCommand);
 
this.Load += new System.EventHandler(this.Page_Load);
 }
 Pagers_PageCommand has an all-important second parameter of type
 PageCommandEventArgs. We use it along with the System.Enum class's static GetName
 
method to produce a textual representation of the PageDirection enumeration value
 
for display in the DirectionLabel Text property:
  
 
private void Pagers_PageCommand(object o, C
 ontrolsBookLib.Ch05.PageCommandEventArgs pce)
 {
 DirectionLabel.Text =
 
Enum.GetName(typeof(ControlsBookLib.Ch05.PageDirection),
 pce.Direction);
 }
 
Click the Left button of the top Pager control to verify that it is working. The result should look something like Figure 5-19.
 
 
 
Figure 5-19. The Page Event Bubbling Web Form after clicking the Left hyperlink button

Try the Right button with the bottom Pager that is in a hyperlink form and you should get output similar to Figure 5-20.
 
 
 
Figure 5-20. The Page Event Bubbling Web Form after clicking the Right hyperlink button

A snippet from the rendered HTML shows that the pager1 and pager2 Pager controls from the Pager Event Bubbling Web Form have their child controls identified in a nested fashion due to the INamingContainer interface with ASP.NET generating the UniqueID property:
 
 
<INPUT type="submit" name="pager1:buttonLeft" id="pager1:buttonLeft"
 
value="&lt; Left"/>&nbsp;&nbsp;
 
<INPUT type="submit" name="pager1:buttonRight" id="pager1:buttonRight"
 
value="Right &gt;"
 <
A href="javascript:__doPostBack('pager2:buttonLeft','')">&lt; Left
 
</A>&nbsp;&nbsp;<A href="javascript:__doPostBack('pager2:buttonRight','')">
 
Right &gt;</A></td></tr></table><br>
 
In the final section of this chapter, we review the control life cycle, which provides orderly processing to the busy life of server controls.
 

Total Pages : 12 89101112

comments