下面就以,.net自動生成的模版做一解釋。(以vb語言爲例)
1.Imports System.ComponentModel
2.Imports System.Web.UI
3.<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> Public Class WebCustomControl1
4. Inherits System.Web.UI.WebControls.WebControl
5. Dim _text As String
6. <Bindable(True), Category("str"), DefaultValue("11111")> Property [Text]() As String
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 導入命名空間,System.ComponentModel和 System.Web.UI 這沒什麼好介紹的
'3 DefaultProperty("Text")--指定屬性的默認值。如果用此屬性需要導入(命名空間: System.ComponentModel)
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")
指定當從 Visual Studio 等工具中的工具箱拖動自定義控件時爲它生成的默認標記。
在下面的示例中,設置特定於 MyLabel 的若干屬性。{0} 的所有匹配項都由設計器替換爲與 MyLabel 類關聯的標記前綴。
<ToolboxData("<{0}:MyLabel Text='MyLabel' BorderColor='Yellow' BackColor='Magenta' BorderWidth = '10' runat='server'></{0}:MyLabel>")>
Public Class WebCustomControl1定義類名爲webcustomcontrol1,以後編譯生成的dll名爲webcustomtrol1
(注意:如果你修改類名。則需要修改{0}:後相對應的名字。例如:你把類名webcustomcontrol1改爲webcustom。
則需要把ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")改成
ToolboxData("<{0}:webcustom runat=server></{0}:webcustom>") 否則編譯後將出錯。)
'4 Inherits 表示繼承。這裏是繼承System.Web.UI.WebControls.WebControl的方法,屬性,事件等。
'6 這句主要是控制自定義控件在’屬性瀏覽器‘中的顯示,先解釋模版的句子,再擴展開講
Property [Text]() As String定義 text屬性 爲字符串類型
Bindable(True)指定是否要綁定到該屬性。-True爲是,False爲不
Category("Appearance") --text屬性將顯示在外觀組中。指定類別的名稱,在該類別中將對屬性或事件進行分組。當使用了類別時,組件屬性和事件可以按邏輯分組顯示在屬性瀏覽器中。
DefaultValue("")爲屬性設置一個簡單的默認值。這裏爲空
下面列出所有的特性
詳細資料可查看ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpcondesign-timeattributesforcomponents.htm
屬性
應用於
說明
BrowsableAttribute
屬性和事件
指定屬性或事件是否應該顯示在屬性瀏覽器中。
CategoryAttribute
屬性和事件
指定類別的名稱,在該類別中將對屬性或事件進行分組。當使用了類別時,組件屬性和事件可以按邏輯分組顯示在屬性瀏覽器中。
DescriptionAttribute
屬性和事件
定義一小塊文本,該文本將在用戶選擇屬性或事件時顯示在屬性瀏覽器底部。
BindableAttribute
屬性
指定是否要綁定到該屬性。
DefaultPropertyAttribute
屬性
(將此特性插入類聲明前。)
指定組件的默認屬性。當用戶單擊控件時,將在屬性瀏覽器中選定該屬性。
DefaultValueAttribute
屬性
爲屬性設置一個簡單的默認值。
EditorAttribute
屬性
指定在可視設計器中編輯(更改)屬性時要使用的編輯器。
LocalizableAttribute
屬性
指定屬性應本地化。當用戶要本地化某個窗體時,任何具有該特性的屬性都將自動永久駐留到資源文件中。
DesignerSerializationVisibilityAttribute
屬性
指定顯示在屬性瀏覽器中的屬性是否應該(以及如何)永久駐留在代碼中。
TypeConverterAttribute
屬性
指定將屬性的類型轉換爲另一個數據類型時要使用的類型轉換器。
DefaultEventAttribute
事件
(將此特性插入類聲明前。)
指定組件的默認事件。這是當用戶單擊組件時在屬性瀏覽器中選定的事件。
.net中還支持自定義特性,這裏就不說,有興趣的可以去查msdn,上面有詳細說明
可參考ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconwritingcustomattributes.htm
7-12很簡單,意思就是返回(Get)Text屬性的值和設置(Set)Text屬性的值
13 Text屬性的結束
14-16 這個過程作用是重寫控件的呈現。這裏是在頁面上顯示Text屬性的值
在asp.net中當你想對button的click事件做確認操作,但Button按鈕不能滿足此要求。就針對此要求來編寫自己的控件。
======================================================================
繼承:System.Web.UI.WebControls.Button
控件功能:彈出確認消息框
控件屬性:message(消息框中顯示的信息)
控件方法:不需要
控件事件:不需要
使用方法:“確定”執行按鈕的button_click事件,“取消”不執行任何事件。
Imports System.ComponentModel
Imports System.Web.UI
Namespace WebControls
<DefaultProperty("Text"), ToolboxData("<{0}:ConfirmButton runat=server></{0}:ConfirmButton>")> Public Class ConfirmButton
'繼承button
Inherits System.Web.UI.WebControls.Button
'爲其所包含的任何服務器控件提供唯一的命名空間
Implements INamingContainer
Dim _Message As String
'定義message屬性。
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Message]() As String
Get
Return _Message
End Get
Set(ByVal Value As String)
_Message = Value
End Set
End Property
Public Sub New()
_Message = ""
End Sub
'重寫控件的輸出
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
'爲控件增加客戶端onclick事件。
If Me.Message.Trim <> "" Then Me.Attributes.Add("onClick", "jscript:if(!confirm('" & Me.Message & "')) return false;")
Me.Attributes.Add("onFocus", "jscript:this.blur();")
MyBase.Render(output)
End Sub
End Class
End Namespace