Transform the ASP.NET user control to custom control

xiaoxiao2021-04-07  348

Transform the ASP.NET user control to custom control

Author:

Sugar | Tags:

C # | Browse (389) | Comments (0) | 03-13 10:43

Author: Kevin Cheng (Cheng and construction) Last Modified: 2006-03-12 Overview: How to ASP.NET user controls for ASP.NET custom control transplantation Keywords: Asp.net, user controls, custom controls applicable to this article Reader: - Familiar with the ASPNET, you can create ASCX User Controls - Want to create a custom control, while the readers who are afraid of their prayer implementations: - ASCX control routines used herein: WebFTP060308 (PM06) .rar - this article Custom control routines: Webftp 1.0.2259.39656.rar terminology: Field class member variable Property class attribute Attribute class feature Namespace namespace

Introduction: ASP.NET custom control is quite convenient to use, but relative to ASCX user controls, due to its no visual programming support, it is quite difficult. Is there a simple way? You can use the advantages of ASCX visualization programming, but also use the convenient features of custom controls. This article describes a solution: first design ASCX control, and then transform into user controls. The following is a specific step. First, create an ASCX control, and debugging how to create an ASCX user control is not the focus of this article, skip ~ The ASCX routine used in this article can click here to download. This routine creates a web file management ASCX control. Effects such as:

Second, transplant the ASCX to Custom Control (1) New Engineering ASPNET Custom Control Engineering, copy all CS code in the original ASCX class to the custom control class (2) Modify the InitializationComponent () function to initialize the control. It can be extended to three small functions, such as:

Private

Void

InitializationComponent () {createcomponent (); setcomponentproperty (); setcomponentevent ();

1

CreateComponent () function is used to create all the controls used, and organize their hierarchy relationships.

Private

Void

CreateComponent () {

//

Create

LBLCurrPath

=

New

Label (); lblupload

=

New

Label (); lblnewfolder

=

New

Label ();

//

Nest

Controls.add

THIS

.lblcurrpath; controls.add (

New

HtmlGenericControl

"

Br

"

));

THIS

.panadmin.controls.add (

THIS

.file);

2

The setComponentProperty () function is used to set the properties of these controls, such as

Private

Void

Setcomponentproperty () {lblcurrpath.font.size

=

FontUnit.xlarge; lblcurrpath.cssclass =

"

Webftp_title

"

Paninfo.backcolor

=

Color.Yellow; Paninfo.visible

=

False

It is worth noting that the combined binding control provided by the ASPNET is more complicated, see Appendix 2 (

3

) SetComponEntevent () Settings the event handle of the control, this is copied from the original InitializationComponent function, such as

Private

Void

Setcomponentevent () {

THIS

.btnupload.click

=

New

System.eventhandler

THIS

.btnupload_click;

THIS

.btnnewfolder.click

=

New

System.eventhandler

THIS

.btnnewfolder_click;

THIS

.dg.ItemCommand

=

New

System.Web.ui.WebControls.DataGridCommandeventHandler

THIS

.dg_itemcommand;

THIS

.Load

=

New

System.eventhandler

THIS

.Page_load);

(3) Fill the render () function to render the output effect. It can be extended to two small functions, such as

protected

Override

Void

Render (HTMLTextWriter Output) {renderclientScript (OUTPUT); renderServerComponent (OUTPUT);

1

RenderClientScript function can be used to output client scripts, as well as style style code, such as:

Private

Void

RenderClientScript (HTMLTextWriter output) {Output.write

"