Creating and Using Composite Controls

   

This example showsyou how to write a control that composes itself from other server controls. The capability to do this is especially useful if you already have controls with functionality that you need and you just want to create a super-class that does much more. Listing 13.10 shows the control's source code, and Listing 13.11 shows the .aspx file that uses it.

Listing 13.10 The Control's Code Composed from Other Server Controls
 namespace MyContro  {  [ShowInToolbox(true),      ToolboxData("<{0}:Composition1 runat=server></{0}:Composition1>")]  public class Composition1 : System.Web.UI.WebControls.WebControl,  INamingContainer  {      public int Value      {         get         {             return Int32.Parse(((TextBox)Controls[1]).Text);         }         set         {             ((TextBox)Controls[1]).Text = value.ToString();         }      }      protected override void CreateChildControls()      {          this.Controls.Add(new LiteralControl("<h3>Value: "));          TextBox box = new TextBox();          box.Text = "0";          this.Controls.Add(box);          this.Controls.Add(new LiteralControl("</h3>"));      }  }  } 
Listing 13.11 The ASP.NET Page That Uses the Example
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <script language="VB" runat="server">        Private Sub AddBtn_Click(Sender As Object, E As EventArgs)            ThisControl.Value = ThisControl.Value + 1        End Sub        Private Sub SubtractBtn_Click(Sender As Object, _            E As EventArgs)            ThisControl.Value = ThisControl.Value - 1        End Sub     </script>     <body>        <form method="POST" action="Composition1.aspx" runat=server>            <CustomControl:Composition1 id="MyControl"                 runat="server"/>            <br>            <asp:button text="Add" OnClick="AddBtn_Click"                runat="server"/>            <asp:button text="Subtract" OnClick="SubtractBtn_Click"                runat="server"/>        </form>     </body>  </html> 

Handling Postback Events with Controls Composed from Other Server Controls

Listings 13.12 and 13.13 provide examples of writing a control that composes itself from other server controls and then uses those server controls to sync and handle postback events (in this case, click events). The control handles postback events that are generated from the user interface.

Listing 13.12 A Control That Handles Postback Events
 namespace MyControl  {  [ShowInToolbox(true),       ToolboxData("<{0}:Composition2 runat=server></{0}:Composition2>")]  public class Composition2 : System.Web.UI.WebControls.WebControl,  INamingContainer  {      public int Value      {         get         {             return Int32.Parse(((TextBox)Controls[1]).Text);         }         set         {             ((TextBox)Controls[1]).Text = value.ToString();         }      }      protected override void CreateChildControls()      {         // Add Literal Control         this.Controls.Add(new LiteralControl("<h3>Value: "));         // Add Textbox         TextBox box = new TextBox();         box.Text = "0";         this.Controls.Add(box);         // Add Literal Control         this.Controls.Add(new LiteralControl("</h3>"));         // Add "Add" Button         Button addButton = new Button();         addButton.Text = "Add";         addButton.Click += new EventHandler(this.AddBtn_Click);         this.Controls.Add(addButton);         // Add Literal Control         this.Controls.Add(new LiteralControl("  "));         // Add "Subtract" Button         Button subtractButton = new Button();         subtractButton.Text = "Subtract";         subtractButton.Click += new EventHandler(this.SubtractBtn_Click);         this.Controls.Add(subtractButton);      }      private void AddBtn_Click(Object sender, EventArgs e)      {         this.Value++;      }       private void SubtractBtn_Click(Object sender, EventArgs e)      {         this.Value--;      }  }  } 
Listing 13.13 The ASP.NET Page That Consumes the Example Control
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <body>        <form method="POST" action="Composition2.aspx" runat=server>            <CustomControl:Composition2 id="ThisControl"                  runat="server"/>        </form>     </body>  </html> 

Exposing Events to a Page (Using Control Composition)

Now I'll write a control that composes itself from other server controls, uses those server controls to sync and handle postback events, and then exposes and raises custom events to page developers. Listings 13.14 and 13.15 show the code. The OnChange() method provides the main functionality. Responding to this event gives developers a way to exercise more control over classic HTML because they are notified when a change has been made.

Listing 13.14 The Code for the Control That Exposes Events
 namespace MyControl  {  [ShowInToolbox(true),      ToolboxData("<{0}:Composition3 runat=server></{0}:Composition3>")]  public class Composition3 : System.Web.UI.WebControls.WebControl,  INamingContainer  {      public event EventHandler Change;      public int Value      {         get         {              return Int32.Parse(((TextBox)Controls[1]).Text);         }         set         {             ((TextBox)Controls[1]).Text = value.ToString();         }      }      protected void OnChange(EventArgs e)      {            Change(this, e);      }      protected override void CreateChildControls()      {         // Add Literal Control         this.Controls.Add(new LiteralControl("<h3>Value: "));         // Add Textbox         TextBox box = new TextBox();         box.Text = "0";         box.TextChanged += new EventHandler(this.TextBox_Change);         this.Controls.Add(box);         // Add Literal Control         this.Controls.Add(new LiteralControl("</h3>"));         // Add "Add" Button         Button addButton = new Button();         addButton.Text = "Add";         addButton.Click += new EventHandler(this.AddBtn_Click);         this.Controls.Add(addButton);         // Add Literal Control         this.Controls.Add(new LiteralControl("  "));         // Add "Subtract" Button         Button subtractButton = new Button();         subtractButton.Text = "Subtract";         subtractButton.Click += new EventHandler(this.SubtractBtn_Click);         this.Controls.Add(subtractButton);      }      private void TextBox_Change(Object sender, EventArgs e)      {         OnChange(EventArgs.Empty);      }      private void AddBtn_Click(Object sender, EventArgs e)      {         this.Value++;         OnChange(EventArgs.Empty);       }      private void SubtractBtn_Click(Object sender, EventArgs e)      {         this.Value--;         OnChange(EventArgs.Empty);      }  }  public class Label: System.Web.UI.WebControls.WebControl  {     public String Text     {        get        {            return (String) State["Text"];        }        set        {            State["Text"] = value;        }     }     public int FontSize     {        get        {            return (int) State["FontSize"];        }        set        {            State["FontSize"] = value;        }     }     protected override void Render(HtmlTextWriter output)     {         output.Write("<font size=" + this.FontSize + ">" + this.Text + "</font>");     }  }  } 
Listing 13.15 The ASP.NET Code That Uses the Example
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <script language="VB" runat=server>        Private Sub Composition2_Change(Sender As Object, _           E As EventArgs)           If ThisControl.Value < 0              ThisControl.Value = 0           End If        End Sub     </script>     <body>        <form method="POST" action="Composition3.aspx" runat=server>            <CustomControl:Composition3 id="ThisControl"                OnChange="Composition2_Change" runat="server"/>        </form>     </body>  </html> 
   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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