Server control learning - bubbling

xiaoxiao2021-03-06  107

Server control learning - bubbling

ASP.NET Page Framework provides "event bubbling" technology. Allow sub-controls to spread events along their tolerance hierarchy. Event bubbles allow events to raise events in the control hierarchy, and allows event handler to attach the event handler to the control of the original control and the control of the public bubble.

Data Binding Controls (Repeater, Datalist, and DataGrid) Using events Bubbling will open a sub-control (in the project template) command event as a top event. Although the ASP.NET server control in .NET Framework will use events for command events (event data classes are derived from CommandEventArgs) but any events defined on the server control can bubbber.

The control can participate in the event bubbling by two methods inherited from the base class system.web.ui.control. These two methods are: OnbubbleEvent and RaisebubbleEvent Overridable Protected Function OnbubbleEvent (Byval Source AS)

Object, byval args as evenetargs) as boolean protected sub raisebubbleevent (Byval Source As Object, Byval Args As Eventargs)

The implementation of the RaisebubbleEvent is provided by Control and cannot be rewritten. RaisebubbleEvent sends event data to the parent of the control along the hierarchy. To handle or trigger the bubble event, the control must override the OnbubbleEvent method.

Controls are handled and continue to bubbber events. To achieve this, the control must override OnbubbleEvent and call RaisebubbleEvent from OnbubbleEvent.

Protected Overrides Function OnBubbleEvent (source As Object, e As EventArgs) As Boolean If TypeOf e Is CommandEventArgs Then 'Adds information about an Item to the' CommandEvent. Dim args As New TemplatedListCommandEventArgs (Me, source, CType (e, CommandEventArgs)) RaiseBubbleEvent (ME, ARGS) RETURN TRUE END IF RETURN FALSE END FUNCTION

The control stops the event bubbling and triggered and / or handling the event. Starting events require calling to schedule events to listeners. To trigger the bubbling event, the control must override OnbubbleEvent to call the OneventName method that triggers this bubbling event. Controls that cause bubbling events typically open the bubbling events as top events. The following code snippet triggers an affailed event.

Protected Overrides Function OnBubbleEvent (source As Object, e As EventArgs) As Boolean Dim handled As Boolean = False If TypeOf e Is TemplatedListCommandEventArgs Then Dim ce As TemplatedListCommandEventArgs = CType (e, TemplatedListCommandEventArgs) OnItemCommand (ce) handled = True End If Return handled End Function Note Although the ONBubbleEvent that enables event bubbling is in line with the standard .NET Framework naming mode that causes events, it is not called BubbleEvent. In the control of the event bubbling, the bubbling event is disclosed as a top event. For example, the DataList control publics the Command event of the control in its template to the ItemMand event. Also note that the standard signature of the oneventname method in .NET Framework has a parameter (EventArgs E)).

However, OnbubbleEvent has two parameters because the event originated from the control; the second parameter provides the source.

Bubbling defined event Protected Overridable Sub OnCommand (e As CommandEventArgs) Dim handler As CommandEventHandler = CType (Events (EventCommand), CommandEventHandler) 'This statement read' a delegate is assumed commandeventhandler ' Public Delegate Sub CommandEventHandler ( Byval e AS Object, byval e as commandeventargs) "Can you use DIM to instance a variable instance be a delegate? 'Is it usually not only declared with Event? Public Event command as commandeventhandler' additional Events, what is EventCommand? If you can't find the answer ', you can replace it with a delegation? If not (Handler is nothing) THE COMMAND END IPED UP The Control Hierarchy. RaisebubbleEvent (ME, E) End Sub

Full code:

[Visual Basic] Option ExplicitOption Strict

Imports SystemImports System.Webimports System.Web.uiimports 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 ( "

Enter a number: ")) _box1 = new textbox () _box1.text =" 0 "controls.add (_box1) Controls.add (" ")) Controls.add (New LitralControl)

Enter Another Number: ")) _box2 =

New textbox () _box2.text = "0" controls.add (_box2) Controls.add (")) 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 ( "

")) _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" ThenSubmit (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 Classend Namespace After repeated learning, there are still a few questions, only to put it first, then seek the answer later.

转载请注明原文地址:https://www.9cbs.com/read-102667.html

New Post(0)