Generally, the writing of event categories is simple, but in ASP. the events generated by interacting with front-end users in ASP. netProgramWrite true object orientation, and client users' operations generate corresponding events through PostBack. For example, the front-end user button will trigger the Click Event of the servo-side button. When the front-end user enters the text box and leaves, the textchanged event of the servo-side textbox will be triggered, this article describes how to use PostBack to generate events that interact with users.
I,IpostbackeventhandlerAnd IpostbackdatahandlerInterface
The control must implement the ipostbackeventhandler or ipostbackdatahandler interface to process events generated by PostBack. What are the differences between the two interfaces? For example, a button is implemented as an ipostbackeventhandler interface, and the PostBack generated by the control directly triggers an event, such as a button click event. For example, textbox is implemented as the ipostbackdatahandler interface. When the page generates a PostBack, the new value entered by the client is passed to the control, which determines whether to trigger the event. For example, Textbox, the textchanged event is triggered only when the new value and the old value are different.
II,IpostbackeventhandlerInterface implementation
First, we will introduce the ipostbackeventhandler interface, which contains the raisepostbackevent method. The control must process the events to be triggered in this method. We inherit webcontrol and write mybutton categories to describe the ipostbackeventhandler interface. we simplify the control program.CodeEnter the original HTML code of the button directly in the render method and define a click event. Implements the raisepostbackevent method of the ipostbackeventhandler interface, which directly triggers the click event.
<Toolboxdata ("<{0}: mybutton runat = Server> </{0}: mybutton>")> _
Public ClassMybutton
InheritsWebcontrol
ImplementsIpostbackeventhandler
''' <Summary>
'''Click event.
''' </Summary>
Public EventClickAsEventhandler
''' <Summary>
''' Triggers a click event.
''' </Summary>
Private SubOnclick (ByvalEAsEventargs)
RaiseeventClick (Me, E)
End Sub
Public SubRaisepostbackevent (ByvalEventargumentAs String)ImplementsSystem. Web. UI. ipostbackeventhandler. raisepostbackevent
DimEAs NewEventargs ()
Onclick (E)'Issue a click event
End Sub
''' <Summary>
''' Override the render method.
''' </Summary>
Protected Overrides SubRender (ByvalWriterAsSystem. Web. UI. htmltextwriter)
DimShtmlAs String
Shtml =String. Format ("<Input type = submit name = {0} value = 'Press quota'/>",Me. Uniqueid)
Writer. Write (shtml)
End Sub
EndClass
Drag the mybutton control on the page and find the click event in the Properties window. Click 2 to generate the click event processing function, and write the test program code as follows.
Protected SubMybutton#click (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesMybutton1.click
Me. Page. response. Write ("Mybutton Click Event")
EndSub
Execute the program. When you press the mybutton button, it will execute its raisepostbackevent method to trigger the click event, and then execute the click event processing function program code.
III,IpostbackdatahandlerInterface implementation
The ipostbackdatahandler Interface contains the loadpostdata and raisepostdatachangedevent methods. When the page is PostBack, you will find the control with the ipostbackdatahandler interface and first execute the loadpostdata method, in the loadpostdata method, the control determines whether to trigger the event based on the value passed by the client. If loadpostdata returns true, it indicates that the event is to be triggered. In this case, the control executes the raisepostdatachangedevent Method to Determine the events to be triggered, otherwise, false indicates that the event is not triggered.
We inherit webcontrol and write mytext categories to describe the ipostbackdatahandler interface. we simplify the control program code to directly input the HTML original code of the text box in the render method and define a textchanged event. In the loadpostdata method, we will determine whether the input value of the client is different from the current value. If the input value is different, true is returned. In this case, the raisepostdatachangedevent method is executed, and the textchanged event is triggered.
<Toolboxdata ("<{0}: mytextbox runat = Server> </{0}: mytextbox>")> _
Public ClassMytextbox
InheritsWebcontrol
ImplementsIpostbackdatahandler
Public PropertyText ()As String
Get
Return Ctype(Me. Viewstate ("Text"),String)
End Get
Set(ByvalValueAs String)
Me. Viewstate ("Text") = Value
End Set
End Property
''' <Summary>
'''Textchanged event.
''' </Summary>
Public EventTextchangedAsEventhandler
''' <Summary>
''' Triggers the textchanged event.
''' </Summary>
Private SubOntextchanged (ByvalEAsEventargs)
RaiseeventTextchanged (Me, E)
End Sub
Public FunctionLoadpostdata (ByvalPostdatakeyAs String,ByvalPostcollectionAsSystem. Collections. Specialized. namevaluecollection)As Boolean ImplementsSystem. Web. UI. ipostbackdatahandler. loadpostdata
'Frontend user quota entry value
DimOnewvalueAs String= Postcollection. Item (postdatakey)
IfOnewvalue <>Me. TextThen
Me. Text = onewvalue
Return True
Else
Return False
End If
End Function
Public SubRaisepostdatachangedevent ()ImplementsSystem. Web. UI. ipostbackdatahandler. raisepostdatachangedevent
DimEAs NewEventargs ()
'Send textchanged event
Ontextchanged (E)
End Sub
''' <Summary>
''' Override the render method.
''' </Summary>
Protected Overrides SubRender (ByvalWriterAsSystem. Web. UI. htmltextwriter)
DimShtmlAs String
Shtml =String. Format ("<Input type = text name = {0} value = {1}>",Me. Uniqueid,Me. Text)
Writer. Write (shtml)
End Sub
EndClass
Drag mytext on the pageBThe following code is written for the ox and mybutton controls in the click event of mybutton and the textchanged event of mytextbox.
Protected SubMybutton#click (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesMybutton1.click
Me. Page. response. Write ("Mybutton Click Event")
Me. Page. response. Write ("<Br/>")
End Sub
Protected SubMytextbox1_textchanged (ByvalSenderAs Object,ByvalEAsSystem. eventargs)HandlesMytextbox1.textchanged
Me. Page. response. Write ("Mytextbox textchanged event")
Me. Page. response. Write ("<Br/>")
EndSub
Execute the program. If mytextbox does not input a value for the first time, press mybutton directly. In this case, only the mybutton click event is triggered.
Enter "A" in mytextbox for the second time, and then press mybutton. Because the value of mytextbox is "Null String->" A ", the textchanged event of mytextbox and the click event of mybutton are triggered.
Note: This article is also published in "the first it state help tie Ren Competition". If you think this articleArticleIt is helpful to you. Remember to add popularity when you connect to this article ^
Http://ithelp.ithome.com.tw/question/10011861