The following sample develops a composite control that combines four ASP.NET server controls: two Textbox controls, a Label control, and a Button control. When its child Button control is clicked,
Note that the child controls of a composite control are encapsulated. By default, they are not visible outside the parent. (A page developer could try to access the child controls using the parent's Controls collection, but because of intervening literal controls it might be difficult to obtain the index of a particular child control.)
A composite control can choose whether to expose a child control as a property and also which properties and events of its child controls to expose as top-level properties and events. When a composite control synthesizes properties from those of its child controls, it merely delegates to the child controls, as shown in the following example.
The code for the composite control sample follows. To build the sample, see the instructions in Server Control Samples.
The following sample uses the composite control
Composite
checks whether the sum of the two numbers entered in the text boxes
equals a specified number (defined as a custom property) and raises a
custom event. It also exposes the Text property of its child Label control as a top-level property.Note that the child controls of a composite control are encapsulated. By default, they are not visible outside the parent. (A page developer could try to access the child controls using the parent's Controls collection, but because of intervening literal controls it might be difficult to obtain the index of a particular child control.)
A composite control can choose whether to expose a child control as a property and also which properties and events of its child controls to expose as top-level properties and events. When a composite control synthesizes properties from those of its child controls, it merely delegates to the child controls, as shown in the following example.
// Delegate to label, which is an instance of // System.Web.UI.WebControls.Label. public string Text { get { EnsureChildControls(); return label.Text; } set { EnsureChildControls(); label.Text = value; } }
Composite
exposes the following public properties.
Number
A custom property that allows a page developer to specify a number.Text
A property synthesized out of the Text property of the child Label control.
Composite
exposes the following custom event.
Check
A custom event that is raised whenComposite
checks whether the sum of the two numbers in the text boxes equals the value ofNumber
. TheCheck
event requires the custom event delegate,CheckEventHandler
, and the corresponding class for event data,CheckEventArgs
. For information about defining a custom event, see Defining an Event.
Composite
control.
Composite
creates its child controls in the CreateChildControls method and not in OnInit or in its constructor.Composite
does not expose the Click event of its Button child control. It instead handles the Click event and raises the custom eventCheck
. If a composite control handles events raised by its child controls, it must wire the event handlers in CreateChildControls.Composite
implements INamingContainer to route a postback event to its child Button control.
The code for the composite control sample follows. To build the sample, see the instructions in Server Control Samples.
/ Composite.cs. using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CustomControls { public class Composite : Control, INamingContainer { private int number = 100; private Label label; public int Number { get { return number; } set { number = value; } } private int Sum { get { EnsureChildControls(); return Int32.Parse(((TextBox)Controls[1]).Text) + Int32.Parse(((TextBox)Controls[4]).Text); } } public string Text { get { EnsureChildControls(); return label.Text; } set { EnsureChildControls(); label.Text = value; } } public event CheckEventHandler Check; protected virtual void OnCheck(CheckEventArgs ce) { if (Check != null) { Check(this,ce); } } protected override void CreateChildControls() { Controls.Add(new LiteralControl("<h3>Enter a number : ")); TextBox box1 = new TextBox(); box1.Text = "0"; Controls.Add(box1); Controls.Add(new LiteralControl("</h3>")); Controls.Add(new LiteralControl("<h3>Enter another number : ")); TextBox box2 = new TextBox(); box2.Text = "0"; Controls.Add(box2); Controls.Add(new LiteralControl("</h3>")); Button button1 = new Button(); button1.Text = "Submit"; Controls.Add(new LiteralControl("<br>")); Controls.Add(button1); button1.Click += new EventHandler(this.ButtonClicked); Controls.Add(new LiteralControl("<br><br>")); label = new Label(); label.Height = 50; label.Width = 500; label.Text = "Click the button to see if you won."; Controls.Add(label); } protected override void OnPreRender(EventArgs e) { ((TextBox)Controls[1]).Text = "0"; ((TextBox)Controls[4]).Text = "0"; } private void ButtonClicked(Object sender, EventArgs e) { OnCheck(new CheckEventArgs(Sum - Number)); } } } // CheckEvent.cs. // Contains the code for the custom event data class CheckEventArgs. // Also defines the event handler for the Check event. using System; namespace CustomControls { public class CheckEventArgs : EventArgs { private bool match = false; public CheckEventArgs (int difference) { if (difference == 0) { match = true; } } public bool Match { get { return match; } } } public delegate void CheckEventHandler(object sender, CheckEventArgs ce); }
The following sample uses the composite control Composite
on an ASP.NET page.
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %> <html>
<script type="text/javascript" runat="server"> private void Sum_Checked(object sender, CheckEventArgs e) { if (e.Match == true) Composite.Text = "You won a million dollars"; else Composite.Text = "Sorry, try again. The numbers you entered don't add up to the hidden number."; } </script> <body> <h1> The Mystery Sum Game </h1><br> <form runat=server> <Custom:Composite id = "Composite" OnCheck = "Sum_Checked" Number= "10" runat = server/> </form> </body> </html>
No comments:
Post a Comment