User controls in ASP.NET (transfer)

xiaoxiao2021-03-06  101

Abstract: User controls provided in ASP.NET can solve the problem that the code reuse in the ASP is more convenient for the error check in debugging. This article uses the user control implementation method and the implementation of a user control routine, further verifying the feasibility and effectiveness of using the user control to resolve code reuse.

[Keyword] code reuse, user control, @ register directive

ASP.NET provides better code separation scheme than traditional ASPs. In a conventional ASP, the code of the ASP file or transaction object component to be executed with Server. Execute is separated, and only the code can only be separated into several files and then use "include". This method will result in a copy of multiple "incrude" files in memory (a file is generated once every time you are referenced), thereby increasing the burden on the system, so it is not feasible. Using Server. Execute may be changed, but the additional query string parameters cannot be passed to the ASP file being executed. Using components may be a relatively better approach. In ASP.NET, in addition to using "include" and transaction object components, two new technologies: Code-BEHIND class and web user controls.

Use the Code-BEHIND class to effectively leave the client's HTML code and server-side event processing code. Divide these two-part code separated the tracker when facilitating debugging, as you don't have to switch between code and transaction logic code that implements user interfaces and transaction logic code. Moreover, if the user interface and transaction processing in the project are developed by different groups, the use of the Code-Behind class can make these two parts to implement independent debugging. Separate user interface code and transaction logic code space to make the code integration and maintenance of the code using the Code-BEHIND class. The user control will be described below to process the code separation problem from the perspective of the code reuse.

1. Introduction to User Controls

User control allows programmers to easily divide and reuse public UI functions across ASP.NET web applications. As with the web form page, programmers can use any text editor to create user controls, or use code hidden class to develop user controls. In addition, like the web form page, the user control can be compiled and stored in the server memory during the first request, thereby shortening the response time of the subsequent request. However, different from the web form page is that the user control cannot be independently requested, and the user control must be included in the web form page.

Compared with the server-side containing file (SSI), user controls are supported by accessing object models provided by ASP.NET to make programmers with greater flexibility. Programmers can program any properties declared in the control, not just the functions provided by other files, which is the same as any other ASP.NET server control.

Although a language is required when you create user controls, programmers can include multiple user controls in a web form page created in multiple languages. For example, you can create a user control with Visual Basic.net, import data from the XML file, and create another user control with C #, which contains an order form, then includes these two in the same web form page. Control.

In addition, the output of the control can be cached independent of the part other than the control in the web form page including the user control. This technology is called a fragment cache, appropriately uses this technology to improve the performance of the site. For example, if the user control contains an ASP.NET server control that proposes a database, but the rest of this page contains only text and simple code running on the server, the programmer can perform fragment cache for the user control to improve the application. performance.

2. Creating a User Control You can create a user control by using a text or HTML editor to declare. User control declaration syntax is very similar to the syntax used by creating a web form page; the main difference between the two is that the user control does not include , , and

elements around the content. These elements are included in the web form page containing the user control. User controls can be as simple as text files, or they can contain other ASP.NET server controls. The following procedure briefly describes a simple login form that can include a plurality of pages of the application. Open attributes, event handles, and other code to include in the user control function in the code declaration block. There are two options when using the properties of the user control. First, you can define new properties of the user control and operate them. Second, the properties of the server control constituting the user control can be operated. For example, declare the TextBox web server control in the user control, and provide an ID to Password, you can operate its text attribute by using the password. Text syntax. [Note] When the user control is included in the web form page, all attributes and methods of any ASP.NET server control included in this user control will increase the public properties and methods of this user control. Step 1: The following code sample declaration maps to the UserID and Password properties of the text box in step 1. These properties can be operated in any web form page that contains this user control by declaring or programming.

Public MustInherit Class LoginformInherits System.Web.UI.UserControl Protected WithEvents User As System.Web.UI.WebControls.TextBox Protected WithEvents Pass As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button #REGION "The code" Generated by the Web Form Design "'This call is required for the web form designer. Private Sub InitializeComponent () End SubPrivate Sub Page_Init (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method is called the Web Form Designer Required 'Don't modify it using the code editor. InitializeComponent () End Sub # End RegionPrivate Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'page where the user code is placed initialization End SubPublic Property UserId () As [String] Get Return User.Text End Get Set (BYVAL VALUE AS [String]) User.Text = Value End SETEND PropertyPublic Property Password () AS [String] Get Return Pass.Text End Get Set (Byval Value As [String]) Pass.text = Value End Setend Propertyend Class Step 2: Create the UI element displayed by the user control. The following code creates a login form that interacts with the code from step 1. [Note] When this user control is included in the web form page, this file name is required in the SRC property of the @REGISTER instruction and the path to the file.

<% @ Control Language = "vb" AutoEventWireup = "false" Codebehind = "Logonform.ascx.vb" Inherits = "logintest.Logonform" TargetSchema = "http://schemas.microsoft.com/intellisense/ie5"%> < Table cellspacing = "15"> User Name: User password: