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. First look at the front desk code of 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: 125p" cellspacing = "1" cellpadding = "1" width = "183" align = "center" border = "1"> < Tr>
We put two Label, two TextBox, two Button and an HTML table. 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, the LoginoutClick event is declared as the control, as follows:
Public Event LoginoutClickHandler LoginoutClick; In addition to better use attributes, add Language enumeration,
Private language language; Of course, the outside is accessed through the Public Language LG {Get; Set;} property. 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. The full code is as follows:
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 CHANGELANGUAGE (LANGUAG = Language;} // Property public language lg {set {if (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 onlineinoutclick (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 logout OnLogInOutClick (this, new LogInOutEventArgs (LogInClickType.LongOut, true));} // verification function private bool CustomValidate (string userName, string password) {// the code verification is omitted, it is assumed by the return true;}}} defines another file Enumeration and parameter class:
using System; namespace ZZ {public class LogInOutEventArgs: EventArgs {private LogInClickType type; private bool result; public LogInOutEventArgs (LogInClickType type, bool result): base () {this.type = type; this.result = result;} public LogInClickType Type {Get {return this.type;}} // operation result, public bool result {get {return this.result;}}} // Operating type public enum LoginclickType: int {longin, longout} // definition language public enum language {Chinese, English}} Take a look at 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 "%>