Reading directory
I. ASP. NET window event processing
Ii. Delegated event processing mode
Iii. Heavy Load event processing mode
I. ASP. NET window event processing
. The event driver is the core of the window program design. If you do not understand the event driver, you cannot enter the window Program Design Hall.
.What is the difference between events in Windows and events in ASP. NET?
In Windows, our machines are single, while our machines are servers and clients. In Windows, you click the button in the server, therefore, we are focusing on event-oriented programming, while ASP. NET, you click the client button to execute the server-side events, although they are server controls that will eventually be displayed in the browser, for one thousand people, 10 thousand people see the same control. When so many people click the button, they actually click it through the client. The last thing they execute is the server-side event. It seems like they click the server-side button, this is actually a simulated process, where a javascript function is used to simulate server-side events.
.There are two main event processing modes in C #, either Windows app or ASP. NET.
-Delegated event processing mode
-Overload event processing mode
Ii. Delegated event processing mode
.A delegated event processing mode is to delegate a specific event to a method responsible for event processing.
.In. NET, a class includes data members (attributes), function members (methods), and event members (events). Event members are the events related to the objects of the class.
.Event processing can be performed through the delegate relationship, and the delegate relationship is used to establish an object. event + = new EventHandle (Method); it means that if an Event occurs in an object, it is handled by the Method. An object can be delegated to multiple different processes, "+ =" is used to add new delegate relationships.
.Method is the place where the event is actually processed, in the format of public void Method (object sender, EventArgs e); object sender refers to the button that triggers the event, that is, the control that triggers the event, eventArgs is. the basic class related to events in NETFramework. All events are encapsulated as EventArgs objects or their subclasses. when an event occurs, Method receives these two parameters, once a delegate relationship is established, the system automatically executes the event handling method you delegate when a specific event occurs.
.Let's take a look at what happened behind the server events.
1: Default. aspx design File
2: Default. aspx. cs code file
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Namespace ServerEvent
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
This. btnTwo. Click + = new EventHandler (this. btnTwo_Click );
}
/// <Summary>
/// Add an event during design. What is "add event during design" is to drag a button from the toolbox and double-click an event to be added.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnOne_Click (object sender, EventArgs e)
{
// This code is actually generated behind the scenes. this. btnOne. Click + = new EventHandler (this. btnOne_Click); associates the Click event with the btnOne_Click event handler.
Response. Write ("add events during design ");
}
/// <Summary>
/// Add an event during runtime. If you have not dragged a button from the toolbox and double-click to add an event, you can add the event at runtime.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnTwo_Click (object sender, EventArgs e)
{
Response. Write ("add event during runtime ");
}
}
}
Iii. Heavy Load event processing mode
.In C #, in addition to defining event members for events related to different classes, the event methods that can be reloaded are also defined, you can design the corresponding event processing by reloading these event methods. To put it bluntly, in C #, many classes provide some event methods that can be reloaded, that is, it has already defined these events, but we just need to redefine them in the derived class.
.These event methods that can be reloaded start with "On" and are declared as "virtual". A method declared as "virtual" indicates that it can be reloaded, the method declared as "onverride" also indicates that it can be reloaded. If declared as "abstract", it indicates that it must be reloaded, for example, the Click Event of a button, there is a corresponding reloaded OnClick event method.
.Reload the button control, derive it into a class, and then reload the required methods. The advantage of a custom control is that it can be used in many places, which is the code repeatability.
We can see from the "Object Browser" which event methods of Web server controls can be reloaded
1: MyCustomButtonControl. cs ASP. NET Server control class
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Linq;
Using System. Text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Namespace ServerControl
{
[DefaultProperty ("Text")]
[ToolboxData ("<{0}: MyButton runat = server> </{0}: MyButton>")]
// "MyButton" refers to my custom Button control. A Custom Button is derived from "Button ".
Public class MyButton: System. Web. UI. WebControls. Button
{
[Bindable (true)]
[Category ("Appearance")]
[DefaultValue ("")]
[Localizable (true)]
Public new string Text
{
Get
{
String s = (String) ViewState ["Text"];
Return (s = null )? "[" + This. ID + "]": s );
}
Set
{
ViewState ["Text"] = value;
}
}
Protected override void OnClick (EventArgs e)
{
This. Page. Response. Write ("overloaded event processing mode ");
}
Protected override void RenderContents (HtmlTextWriter output)
{
Output. Write (Text );
}
}
}
2: add our custom Button control
Select "select item" in the toolbox, and then select the dll compiled by MyCustomButtonControl. cs.
3: Default. aspx design File
Drag "MyButton" to our page