In the early 1990s, Microsoft made the Active Server Pages (ASP) revolution provided by Web programmers changed the Web programming. It can utilize the very easy-to-use model to dynamically generate HTML on the web server, and it is easy to implement access to the database, as at that time, this is an attractive technology, including many web sites on the INTERNET It is written in ASP. My colleagues played more than ASP masters. I have experienced so many years without falling, I can see him. However, the technology is constantly developing, and the reference to a NET expert - Today, the state of Web programming is still behind. Therefore, Microsoft proposed a second-generation programming model --Web form. The web form model is part of the ASP.NET, while ASP.NET is part of the .NET framework. His programming model is based on an event. It is more like a Windows form programming. This is also an important reason I decided to learn to use him. I also watched some books in this area. The purpose of the article is to share experiences with ASP.NET beginners and peers that have not added custom events have been added to user controls. Tony said less, let's build a user control first, here, use a simple login user control to make a demonstration.
Let's look at the front desk Code (LogInOutControl.ascx file) user control: <% @ Control Language = "c #" AutoEventWireup = "false" Codebehind = "LogInOutControl.ascx.cs" Inherits = "ZZ.LogInOutControl" TargetSchema = "http : //schemas.microsoft.com/intellisense/ie5 "%>
| | The next is to add a code for the loginoutcontrol.ascx.cs file. First define a delegate, where the Loginouteventargs class is inherited from Eventargs class, public delegate Void LoginoutClickHandler (Object Sender, Loginouteventargs E); I think this delegate is more appropriate outside the LoginoutControl class. Next to declare the LoginoutClick event, as follows: Public Event loginoutclickhandler loginoutClick; additional use of attributes, adding the language enumeration, Private language language; The purpose is to change or get the display of the current control. Next is to define the control event trigger function OnLoginoutClick, and click the event handler to complete the trigger to the user control event. Following complete code: namespace ZZ {using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; // define a proxy public delegate void LogInOutClickHandler (object sender, LogInOutEventArgs e); public class LogInOutControl: System.Web.UI.UserControl {protected System.Web.UI.WebControls.Button ButtonLogIn; protected System.Web.UI.WebControls.TextBox TextBoxUserName; protected System.Web. UI.WebControls.TextBox TextBoxPassword; protected System.Web.UI.WebControls.Button ButtonLogOut; protected System.Web.UI.WebControls.Label LabelUser; protected System.Web.UI.WebControls.Label LabelPassword; public event LogInOutClickHandler LogInOutClick; private Language Language; // Method Public Void Changeiang (Language Language) {this.lg = Language;} // Property PUBLIC LANGUAGE LG {set {i (value! = this.language) {if (value == Language.English) {this .Labeluser.text = "User:"; This.labelpassword.text = "Password:"; this.buttonlogin.text = "login"; this.buttonlogout.text = "logout";} else {this.labeluser.text = "User:"; this.Labelpassword.text = "Password:"; this.buttonLogin.text = "Login"; this.buttonLogout.Text = "Logout";}}}} private void Page_load (Object Sender, System.EventArgs E) {IF (this.labeluser.text == "User:") this.language = language.english; else this.language = language.CHINESE; } Private void OnLogInOutClick (object sender, LogInOutEventArgs e) {if (LogInOutClick = null!) LogInOutClick (this, e);} #region Web Form Designer generated code override protected void OnInit (EventArgs e) {InitializeComponent (); base.OnInit (e);} private void InitializeComponent () {this.ButtonLogIn.Click = new System.EventHandler (this.ButtonLogIn_Click); this.ButtonLogOut.Click = new System.EventHandler (this.ButtonLogOut_Click); this. Load = new System.EventHandler (this.Page_Load);} #endregion private void ButtonLogIn_Click (object sender, System.EventArgs e) {OnLogInOutClick (this, new LogInOutEventArgs (LogInClickType.LongIn, CustomValidate (this.TextBoxUserName.Text, this. TextBoxPassword.Text)));} private void ButtonLogOut_Click (object sender, System.EventArgs e) {// Code omitted cancellation OnLogInOutClick (this, new LogInOutEventArgs (LogInClickType.LongOut, true));} // verification function private bool CustomValidate ( String username, string password) {// Verify code omitted, assume return true;}}} Another file defines parameters and enumeration classes: using System; namespace ZZ {public class LogInOutEventArgs: EventArgs {private LogInClickType type; private bool result; public LogInOutEventArgs (LogInClickType type, bool result): base () {This.Type = Type;} public loginclicktype type;}} // operation result, public bool result {get {return this.result;}}} // operation type Public Enum LoginclickType: int {longin, longout} // Define language public enum language {Chinese, English}} Take it to see it in the ASPX page. Create a New Default.aspx page, drag a loginoutcontrol user control to top. <% @ Register TagPrefix = "uc1" TagName = "LogInOutControl" Src = "LogInOutControl.ascx"%> <% @ Page language = "c #" Codebehind = "Default.aspx.cs" AutoEventWireup = "false" Inherits = "ZZ .Default "%> <% @ import namespace =" zz "%> |