自定义asp.net控件分析 (一)
前些日子,自己做了几个控件。想把自己对自定义控件的一些了解写出来,请大家多多发表自己对自定义控件制作的心得。
让我们能更好的动手制作自己所需功能的控件。
下面就以,.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
属性
[1] [2] 下一页 [C语言系列]NET 中C#的switch语句的语法 [Web开发]动易SiteWeaver版自定义采集标签使用说明 [聊天工具]企业形象“自定义”的权利 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [系统软件]自定义DNS访问国外站点 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构
|