Add attributes and events for web user controls in ASP.NET

xiaoxiao2021-03-20  228

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 "%>

User: < ASP: TextBox ID = "TextBoxuserName" width = "128px" runat = "server">
password:
We simply 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 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 "%> Webform1 </ title> </ head> <body> <form id =" form1 "method =" post "runat = "Server"> <font face = "Song"> <uc1: loginoutcontrol id = "loginoutcontrol1" runat = "server"> </ uc1: loginoutcontrol> <ask: label id = "labelmsg" runat = "server"> <server> < / asp: label> <ask: DropdownList ID = "DropDownList1" runat = "server" autopostback = "true"> <ask: listitem value = "0" SELECTED = "true"> Chinese </ asp: listItem> <asp: ListItem value = "1"> English </ asp: listitem> </ asp: DropdownList> </ font> </ form> </ body> </ html> Add events and properties in the background code. Although loginoutControl1 is added at the front desk, protected loginoutcontrol loginoutcontrol1 will not be generated in the background code; this statement, I feel very strange, no matter what he first.</p> <p>Then registered in the Page_Load event LogInOutClick Event: this.LogInOutControl1.LogInOutClick = new LogInOutClickHandler (LogInOutControl1_LogInOutClick); the complete code is as follows: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace ZZ {public class Default: System.Web.UI.Page {protected System.Web.UI.WebControls.Label LabelMsg; protected System.Web.UI.WebControls.DropDownList DropDownList1; protected LogInOutControl LogInOutControl1; private void Page_Load (object sender, System.EventArgs e) {// Register user control events this. LogInOutControl1.LogInOutClick = new LogInOutClickHandler (LogInOutControl1_LogInOutClick);} #region Web form Designer generated code override protected void OnInit (EventArgs e) {InitializeComponent (); base.OnInit (e);} private void InitializeComponent () {this .DropdownList1.selectedIndexc hanged = new System.EventHandler (this.DropDownList1_SelectedIndexChanged); this.Load = new System.EventHandler (this.Page_Load);} #endregion private void LogInOutControl1_LogInOutClick (object sender, LogInOutEventArgs e) {switch (e.Type) {case LoginclickType.longin: this.labelmsg.text = "You click on the login button, the results:" E.Result.tostring (); break; casclicktype.longout: this.labelmsg.text = "You click the logout button, Operation Results: " E.Result.toString (); break;}} private void DropdownList1_selectedIndIndexchanged (Object sender, system.eventargs e) {this.loginoutControl1.lg =</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-130349.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="130349" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.038</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '02DRtr9qMpQowUZwUrd_2F3CasO6MQO2QxzsWnN5Ni7oMGnPpVNgNeHAEuJlCjf6zMBHFxsuVojeYB0dnX'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>