Transform the ASP.NET user control to custom control

xiaoxiao2021-03-17  200

Transform the ASP.NET user control to custom control

Author: Kevin Cheng (Cheng and construction) Last Modified: 2006-03-14 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: WebftpControl 1.0.2259_source.rar term declaration: Field class member variable Property class Properties Attribute class feature Namespace Namespace Assembly assembly query: ASP.NET Custom Control is quite convenient, but relative ASCX users Controls, due to its no visual programming support, it is quite difficult to prepare. 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: 2 Initialization 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

"