In the early 90, Microsoft had revolutionized web programming for Active Server Pages (ASP) provided by web programmers. It can use the very easy-to-use model to dynamically generate HTML on the Web server, and it is easy to access the database, at that time, this is an attractive technology, including the Internet now many Web sites are written in ASP, My colleagues predecessors are playing the Master of ASP, experience so many years without failure, visible his success.
However, technology is constantly evolving, quoting a net expert saying that the state of web programming is still lagging behind. So Microsoft proposes a second-generation programming model ――web form. The Web Forms model is part of the asp.net, and ASP.net is part of the. NET Framework. His programming model is event-based, and it's more like working on Windows Forms programming, which is one of the big reasons I decided to learn to use him, and I read some books about it, The purpose of this article is to share experience with fellow ASP.net beginners and peers who have not yet added custom events to user controls.
Less nonsense, let's start by creating a user control, here is a simple login user control to do the demo.
First take a look at the foreground code for the user control (loginoutcontrol.ascx file):
<%@ Control language= "C #" autoeventwireup= false "codebehind=" LogInOutControl.ascx.cs "inherits=" ZZ. Loginoutcontrol "targetschema=" http://schemas.microsoft.com/intellisense/ie5%>
<table id= "Table1" style = "FONT-SIZE:9PT; width:183px; height:125px "cellspacing=" 1 "
cellpadding=" 1 "width=" 183 "align=" Center "border=" 1 "
<tr>
<td height=
<asp:label id= "Labeluser" runat= "Server" > User: </asp:label>
<asp:textbox id= " Textboxusername "width=" "128px" runat= "Server" ></ASP:TEXTBOX></TD>
</tr>
<TR>
<td height= ><font face= "song"
<asp:label id= "Labelpassword" runat= "Server" > Password: </asp : Label>
<asp:textbox id= "Textboxpassword" width= "128px" runat= "Server" textmode= "Password" ></asp: Textbox></font></td>
</tr>
<tr>
<td align= "center" height= ">< FONT face= "Song" "
<asp:button ID= "Buttonlogin" width= "50px" text= "Login" runat= "server" ></ASP:BUTTON>
<asp:button id= "Buttonlogout" Width= "49px" text= "Logoff" runat= "Server" ></ASP:BUTTON></FONT></TD>
</tr>
</ Table>
We simply put two labels, two textbox, two button and an HTML table.
The next thing is to add code to the LogInOutControl.ascx.cs file.
First, define a delegate, where the Loginouteventargs class inherits from the EventArgs class,
public delegate void Loginoutclickhandler (Object Sender,loginouteventargs e);
I think it's more appropriate to put this delegate outside the Loginoutcontrol class.
Next, the Loginoutclick event is declared for the control, as follows:
public event Loginoutclickhandler Loginoutclick;
In addition, for better use of attributes, the language enumeration is added,
Private Language Language;
Of course externally through the public Language Lg {get;set;} property to access the. The purpose is to change or get the display of the current control.
The next thing is to define the control event trigger function Onloginoutclick, which completes the triggering of the user control event by clicking the event handler function on the button.