Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ASP.NET user Control

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the ASP.NET user control, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Historical Review of ASP and ASP.NET

Although ASP.NET technology evolved from ASP, it is really two different things to use. Let's take a look at the history of these two technologies: in the early 1990s, Active Server Pages (ASP) provided by Microsoft for Web programmers revolutionized Web programming. It can use a very easy-to-use model to dynamically generate HTML on the Web server, and it is easy to access the database. At that time, it was such an attractive technology, including now many web sites on Internet are written in Asp, and my colleagues and predecessors are even more expert at playing Asp. After so many years without decline, we can see his success.

However, the technology is constantly evolving, and to quote a Net expert, the state of Web programming is still lagging behind. Therefore, Microsoft proposed the second generation programming model-Web forms. The Web forms model is part of Asp.net, and Asp.net is part of the .net framework. His programming model is event-based, and using him is more like Windows forms programming, which is an important reason why I decided to learn to use him. I also read some books on it at random. The purpose of writing this article is to share my experience with Asp.net beginners and colleagues who have not yet added custom events to user controls.

Create a simple ASP.NET user control

To cut the crap, let's first build a user control, which is demonstrated with a simple login user control.

Let's first take a look at the foreground code (LogInOutControl.ascx file) of the user control:

< %@ 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="20">

< asp:Label id="LabelUser" runat="server">

Users:

< /asp:Label>

< asp:TextBox id="TextBoxUserName" Width="128px" runat="server">

< /asp:TextBox>

< /TD>

< /TR>

< TR>

< TD height="20">

< FONT face="宋体">

< 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="20">

< FONT face="宋体">

< asp:Button id="ButtonLogIn" Width="50px" Text="登录" runat="server">

< /asp:Button>

< asp:Button id="ButtonLogOut" Width="49px" Text="注销" runat="server">

< /asp:Button>

< /FONT>

< /TD>

< /TR>

< /TABLE>

We simply put two Label, two TextBox, two Button and one Html table.

The next step 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, in order to make better use of attributes, the Language enumeration is added

Private Language language

Of course, the outside is accessed through the public Language Lg {get;set;} property. The goal is to change or get the display of the current control.

The next step is to define the control event trigger function OnLogInOutClick, which is triggered by the button click event handler to trigger the user control event.

The complete code of the ASP.NET user control 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 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 (Language language) {this.Lg = language } / / attribute public Language Lg {set {if (valuevalueconstructhis.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 (object sender,LogInOutEventArgs) LogInOutClick (this,e) } # region Web forms 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) {/ / logout code omits OnLogInOutClick (this,new LogInOutEventArgs (LogInClickType.LongOut,true)) } / / Verification function private bool CustomValidate (string userName,string password) {/ / the verification code is omitted, assuming that through return true;}}

Another file defines enumerations and parameter 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; this.result = result;} public LogInClickType Type {get {return this.type;}} / / Operation result, public bool Result {get {return this.result Operation type public enum LogInClickType: int {LongIn, LongOut} / / definition language public enum Language {Chinese, English}}

Next, look at using the ASP.NET user control in the aspx page.

Create a new Default.aspx page and drag a LogInOutControl user control onto it.

< %@ Register TagPrefix="uc1" TagName="LogInOutControl" Src="LogInOutControl.ascx" %>

< %@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="ZZ.Default" %>

< %@ Import Namespace="ZZ" %>

< HTML>

< HEAD>

< title>

WebForm1

< /title>

< /HEAD>

< body>

< form id="Form1" method="post" runat="server">

< FONT face="宋体">

< uc1:LogInOutControl id="LogInOutControl1" runat="server">

< /uc1:LogInOutControl>

< asp:Label id="LabelMsg" runat="server">

< /asp:Label>

< asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">

< asp: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 to the background code.

Although LogInOutControl1 is added in the foreground, I find it strange that the statement protected LogInOutControl LogInOutControl1; will not be generated in the background code, regardless of adding him first.

Then register the LogInOutClick event in the Page_Load 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) {/ / registered user control event this.LogInOutControl1.LogInOutClick + = new LogInOutClickHandler (LogInOutControl1_LogInOutClick);} # code generated by the region Web forms designer override protected void OnInit (EventArgs e) {InitializeComponent (); base.OnInit (e) } private void InitializeComponent () {this.DropDownList1.SelectedIndexChanged + = 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 clicked the login button, the result of the operation: + e.Result.ToString (); break; case LogInClickType.LongOut: this.LabelMsg.Text =" you clicked the logout button, the result of the operation: + e.Result.ToString () Break;} private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e) {this.LogInOutControl1.Lg = (Language) this.DropDownList1.SelectedIndex; / / this.LogInOutControl1.ChangeLanguage ((Language) this.DropDownList1.SelectedIndex);}

When the user changes the language of the control by selecting the drop-down box list in the foreground, it is done through the Lg property, but a method ChangeLanguage is also added here to achieve the same function. In addition, the LogInOutClick event is triggered by clicking the login or logout button to assign a value to the LabelMsg.Text property on the page to get the result of the operation.

To sum up, user controls not only bring high development efficiency and reusability for programmers, but also have a great improvement in performance. Formerly known as Asp+, I don't think Asp.net is directly related to Asp. And I want to be a friend of the application, like me, when developing Web programs, I prefer code separation, so that the structure is clearer, and it is easier to modify and manage. Compared with Asp programs, he is compiled and compiled, and the idea of object-oriented design is introduced, which inevitably brings his complexity. In order to develop high-level Asp.net programs, we still pay more attention to pattern design and hierarchical structure division. In short, it's more like he's writing a Windows forms program than a VB script.

Thank you for reading this article carefully. I hope the article "how to use ASP.NET user controls" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report