A few days ago, I did several controls. I want to write my own understanding of the custom control, please express my experience in custom controls.
Let us make better handles to make the controls you need.
The template for automatically generated by .NET is explained below. (As an example of VB language)
1. IMPORTS System.comPonentModel
2.imports system.Web.ui
3.
4. Inherits System.Web.ui.WebControls.WebControl
5. DIM _Text As String
6.
7. Get
8. Return_Text
9. End Get
10. SET (ByVal Value As String)
11. _Text = Value
12. End Set
13. End Property
14. Protected Overrides Sub Render (Byval Output As System.Web.ui.htmlTextWriter)
15. Output.write ([Text])
16. End Sub
17.END CLASS
'------------------------------------- ----------------
'1-2 Import Namespace, System.comPonentmodel and System.Web.ui have nothing to introduce
'3 DefaultProperty ("text") - Specifies the default value of the property. If you use this property, you need to import (namespace: system.componentmodel)
ToolboxData ("<{0}: WebcustomControl1 Runat = Server> {0}: WebcustomControl1>")
Specifies the default tag that it generates when the custom control is dragged from the toolbox from Tools such as Visual Studio.
In the following example, set a number of attributes specific to MYLabel. All matching items of {0} are replaced by the designer to a tag prefix associated with MyLabel class.
Public Class WebcustomControl1 defines a class named WebcustomControl1, and later compiles the generated DLL named WebcustomTrol1
(Note: If you modify the class name. You need to modify {0}: The corresponding name. For example, you change the class WebcustomControl1 to WebCustom.
The ToolboxData ("<{0}: WebcuStomControl1 Runat = Server> {0}: Webcustom Runat = Server> {0}: WebCustom>" is required. ) Otherwise, it will be wrong after compiling. )
'4 inherits indicates inheritance. Here is how to inherit system.web.ui.webcontrols.webcontrol, attributes, events, etc.
'6 This sentence is mainly to control the display of custom controls in the' Properties Browser ', first explain the sentence of the template, and expand the opening
Property [Text] () AS String Definition Text Attribute is a string type
Bindable (TRUE) Specifies whether to bind to this property. -True is that False is not
Category ("APPEARANCE") --Text property will appear in the appearance group. The name of the specified category will group attributes or events in this category. When the category is used, the component properties and events can be displayed in the Properties Browser by logical packet.
DEFAULTVALUE ("") Sets a simple default value for the property. Here is empty
All of the features are listed below
Details can be viewed in MS-Help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpcondesign-timeattributesForComponents.htm
Attributes
Use
Description
BrowsableamTribute
Attributes and events
Specifies whether an attribute or event should be displayed in the property browser.
CategoryAttribute
Attributes and events
The name of the specified category will group attributes or events in this category. When the category is used, the component properties and events can be displayed in the Properties Browser by logical packet.
DescriptionAttribute, DESCRIPTIONATTRIBUTE
Attributes and events
Define a small piece of text that will be displayed at the bottom of the property browser when the user selects attributes or events.
BindableAttribute
Attributes
Specifies whether to bind to this property.
DefaultPropertyAttribute
Attributes (before inserting this feature into class declaration.) Specify the default properties of the component. This property will be selected in the Properties Browser when the user clicks the control.
DefaultValueAttribute
Attributes
Set a simple default value for the property.
EditoAttribute
Attributes
Specifies the editor to use when editing (changes) properties in a visual designer.
LocalizableamTribute
Attributes
Specifying properties should be localized. When a user is to localize a form, any properties with this feature will automatically be free to stay in the resource file.
DesignerSerizationVisibilityAttribute
Attributes
Specifies whether the attribute displayed in the property browser should be (and how) permanently reside in the code.
TypeConverTeratRATIBUTE
Attributes
Specifies the type converter to be used when converting the type of attribute to another data type.
DefaultEventAttribute
Event (insert this feature into class declaration.) Specify the default event of the component. This is the event selected in the property browser when the user clicks on the component.
The .NET also supports custom features, this will not be said, interested can check the MSDN, which has a detailed description
Refer to MS-Help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpconwritingcustomattributes.htm
7-12 is very simple, meaning returns the value of the (GET) TEXT attribute and setting (SET) TEXT attribute value
13 TEXT attribute
14-16 This process is to rewrite the presence of the control. Here is the value of displaying the TEXT attribute on the page
Here is just a brief introduction to custom controls. I will use the example to specifically introduce the properties, methods, events in the custom control.
In ASP.NET, when you want to confirm your confirmation of Button's Click event, but the Button button cannot meet this requirement. It is aimed at this request to write your own control.
============================================================================================================================================================================================================= ====================
Inheritance: System.Web.ui.WebControls.Button
Control function: pop-up confirmation message box
Control Properties: Message (information displayed in the Message box)
Control method: no need
Control event: no need
How to use: "OK" The Button_Click event of the execution button, "Cancel" does not perform any events.
Imports system.componentmodel
Imports System.Web.ui
Namespace WebControls
'Inheriting button
Inherits System.Web.ui.WebControls.Button
'Provide unique namespace for any server control included
Implements inamingcontainer
DIM _MESSAGE AS STRING
'Define the Message properties.
Get
Return_Message
END GET
Set (byval value as string)
_Message = Value
End set
End Property
Public Sub New ()
_Message = ""
End Sub
'Override the output of the control
Protected Overrides Sub Render (Byval Output As System.Web.ui.htmlTextWriter)
'Add a client onclick event for the control.
If Me.Message.trim <> "" "" "", "JScript: if (! Confirm ('" & me.Message & ")) Return False;")
Me.attributes.add ("onfocus", "JScript: this.blur ();") MyBase.Render (Output)
End Sub
END CLASS
End Namespace
At this point, the control is written, you see if it is very simple.