次の方法で共有


イベント バブル コントロールのサンプル

カスタム コントロール EventBubbler を使用した単純なイベント バブルの例を次に示します。EventBubbler は、テキスト ボックス、ボタン、およびラベル コントロールを含む複合コントロールです。EventBubbler は、ボタンから親コンテナ コントロール (コントロール自体) にコマンド イベントをバブルし、それらのイベントを最上位イベントとして公開します。このサンプルをビルドする方法については、「サーバー コントロールのサンプル」の手順を参照してください。

より実際的な例については、「template 宣言のあるデータ連結コントロールのサンプル」を参照してください。

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls 
{
   
   public class EventBubbler : Control, INamingContainer 
   {
      private int number = 100;
      private Label label;
      private TextBox box1;
      private TextBox box2;
      
      public event EventHandler Click;
      public event EventHandler Reset;
      public event EventHandler Submit;
      
      public string Label
      {
         get
         {
            EnsureChildControls();
            return label.Text;
         }
         set
         {
            EnsureChildControls();
            label.Text = value;
         }
      }
      
      public int Number
      {
         get
         {
            return number;
         }
         set
         {
            number = value;
         }
      }
      
      public string Text1
      {
         get
         {
            EnsureChildControls();
            return box1.Text;
         }
         set
         {
            EnsureChildControls();
            box1.Text = value;
         }
      }
      
      public string Text2
      {
         get
         {
            EnsureChildControls();
            return box2.Text;
         }
         set
         {
            EnsureChildControls();
            box2.Text = value;
         }
      }
      
      
      protected override void CreateChildControls() 
      {
         
         Controls.Add(new LiteralControl("<h3>Enter a number : "));
         
         box1 = new TextBox();
         box1.Text = "0";
         Controls.Add(box1);
         
         Controls.Add(new LiteralControl("</h3>"));
         
         Controls.Add(new LiteralControl("<h3>Enter another number : "));
         
         box2 = new TextBox();
         box2.Text = "0";
         Controls.Add(box2);
         
         Controls.Add(new LiteralControl("</h3>"));
         
         Button button1 = new Button();
         button1.Text = "Click";
         button1.CommandName = "Click";
         Controls.Add(button1);
         
         Button button2 = new Button();
         button2.Text = "Reset";
         button2.CommandName = "Reset";
         Controls.Add(button2);
         
         Button button3 = new Button();
         button3.Text = "Submit";
         button3.CommandName = "Submit";
         Controls.Add(button3);
         
         Controls.Add(new LiteralControl("<br><br>"));
         label = new Label();
         label.Height = 50;
         label.Width = 500;
         label.Text = "Click a button.";
         Controls.Add(label);
         
      }
      
      protected override bool OnBubbleEvent(object source, EventArgs e) 
      {   
         bool handled = false;
         if (e is CommandEventArgs)
         {
            CommandEventArgs ce = (CommandEventArgs)e;
            if (ce.CommandName == "Click")
            {
               OnClick(ce);
               handled = true;   
            }  
            else if (ce.CommandName == "Reset")
            {
               OnReset(ce);
               handled = true;   
            }
            else if (ce.CommandName == "Submit")
            {
               OnSubmit(ce);
               handled = true;   
            }
            
         }
         return handled;            
      }
      
      protected virtual void OnClick (EventArgs e)
      {
         if (Click != null)
         {
            Click(this,e);
         }
      }
      
      protected virtual void OnReset (EventArgs e)
      {
         if (Reset != null)
         {
            Reset(this,e);
         }
      }
      
      protected virtual void OnSubmit (EventArgs e)
      {
         if (Submit != null)
         {
            Submit(this,e);
         }
      }  
   }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomControls
   Public Class EventBubbler
      Inherits Control
      Implements INamingContainer
      Private _number As Integer = 100
      Private _label As Label
      Private _box1 As TextBox
      Private _box2 As TextBox
      
      Public Event Click As EventHandler
      Public Event Reset As EventHandler
      Public Event Submit As EventHandler
      
      Public Property Label() As String
         Get
            EnsureChildControls()
            Return _label.Text
         End Get
         Set
            EnsureChildControls()
            _label.Text = value
         End Set
      End Property
      
      Public Property Number() As Integer
         Get
            Return _number
         End Get
         Set
            _number = value
         End Set
      End Property
      
      Public Property Text1() As String
         Get
            EnsureChildControls()
            Return _box1.Text
         End Get
         Set
            EnsureChildControls()
            _box1.Text = value
         End Set
      End Property
      
      Public Property Text2() As String
         Get
            EnsureChildControls()
            Return _box2.Text
         End Get
         Set
            EnsureChildControls()
            _box2.Text = value
         End Set
      End Property
      
      Protected Overrides Sub CreateChildControls()
         
         Controls.Add(New LiteralControl("<h3>Enter a number : "))
         
         _box1 = New TextBox()
         _box1.Text = "0"
         Controls.Add(_box1)
         
         Controls.Add(New LiteralControl("</h3>"))
         
         Controls.Add(New LiteralControl("<h3>Enter another number : "))
         
         _box2 = New TextBox()
         _box2.Text = "0"
         Controls.Add(_box2)
         
         Controls.Add(New LiteralControl("</h3>"))
         
         Dim button1 As New Button()
         button1.Text = "Click"
         button1.CommandName = "Click"
         Controls.Add(button1)
         
         Dim button2 As New Button()
         button2.Text = "Reset"
         button2.CommandName = "Reset"
         Controls.Add(button2)
         
         Dim button3 As New Button()
         button3.Text = "Submit"
         button3.CommandName = "Submit"
         Controls.Add(button3)
         
         Controls.Add(New LiteralControl("<br><br>"))
         _label = New Label()
         _label.Height = Unit.Pixel(50)
         _label.Width = Unit.Pixel(500)
         _label.Text = "Click a button."
         Controls.Add(_label)
      End Sub
       
      Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
         Dim handled As Boolean = False
         If TypeOf e Is CommandEventArgs Then
            Dim ce As CommandEventArgs = CType(e, CommandEventArgs)
            If ce.CommandName = "Click" Then
               OnClick(ce)
               handled = True
            Else
               If ce.CommandName = "Reset" Then
                  OnReset(ce)
                  handled = True
               Else
                  If ce.CommandName = "Submit" Then
                     OnSubmit(ce)
                     handled = True
                  End If
               End If 
            End If
         End If
         Return handled
      End Function
      
      Protected Overridable Sub OnClick(e As EventArgs)
         RaiseEvent Click(Me, e)
      End Sub
      
      Protected Overridable Sub OnReset(e As EventArgs)
         RaiseEvent Reset(Me, e)
      End Sub
      
      Protected Overridable Sub OnSubmit(e As EventArgs)
         RaiseEvent Submit(Me, e)
      End Sub
   End Class
End Namespace

ページでのイベント バブル コントロールの使用

カスタム イベント バブル コントロール EventBubbler を使用し、イベント ハンドラをその最上位イベントにアタッチする ASP.NET ページを次に示します。

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server>

        private void ClickHandler(Object sender,EventArgs e)
        {
           MyControl.Label = "You clicked the <b> Click </b> button";

   }

        private void ResetHandler(Object sender,EventArgs e)
        {
          MyControl.Text1 = "0";
          MyControl.Text2 = "0";       

   }     

   private void SubmitHandler(Object sender,EventArgs e)
        {
          if ( Int32.Parse(MyControl.Text1) + Int32.Parse(MyControl.Text2) == MyControl.Number)
           MyControl.Label = "<h2> You won a million dollars!!!! </h2>";
          else 
           MyControl.Label = "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:EventBubbler id = "MyControl" OnClick = "ClickHandler" 
OnReset = "ResetHandler" OnSubmit = "SubmitHandler" Number= "10" runat = server/>                                     
</form>                       
</body>                    
</html>                  
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="VB" runat=server>

    Private Sub ClickHandler(sender As Object, e As EventArgs)
       MyControl.Label = "You clicked the <b> Click </b> button"
    End Sub
     

    Private Sub ResetHandler(sender As Object, e As EventArgs)
       MyControl.Text1 = "0"
       MyControl.Text2 = "0"
    End Sub
     

    Private Sub SubmitHandler(sender As Object, e As EventArgs)
       If Int32.Parse(MyControl.Text1) + Int32.Parse(MyControl.Text2) = MyControl.Number Then
          MyControl.Label = "<h2> You won a million dollars!!!! </h2>"
       Else
          MyControl.Label = "Sorry, try again. The numbers you entered don't add up to" & " the hidden number."
       End If
    End Sub 
   
</script>
      
<body>

<h1> The Mystery Sum Game </h1><br>
         
<form runat=server>              
<Custom:EventBubbler id = "MyControl" OnClick = "ClickHandler" 
OnReset = "ResetHandler" OnSubmit = "SubmitHandler" Number= "10" runat = server/>                                     
</form>                       
</body>                    
</html>

参照

イベントのバブル | template 宣言のあるデータ連結コントロールのサンプル