下面就以,.net自动生成的模版做一解释。(以vb语言为例)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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
分享到:
相关推荐
自定义等待服务器控件,提示和确认控件,进度条控件等.
自定义asp.net翻页控件,功能非常强大,应用非常方便,只需在工具箱中添加即可和系统中其他控件一样使用。
asp.net 自定义控件 抽屉控件asp.net 自定义控件 抽屉控件
自定义 控件开发 asp.net控件 复合控件
asp.net 自定义控件 上图文件控件 带有进度和时间 高效率
自定义asp.net中DataGrid的一个分页控件。。 在winform下实现。。。其实有了思路哪儿写都一样O(∩_∩)O哈哈~
asp.net GridView自定义控件源码,美化控件。
一款自定义Asp.Net2.0分页控件,具有3.5中DataPager相同的功能。
ASP.NET自定义控件开发实例教程,通过一系列具体的例子展示了如何创建从简单的到复杂的数据绑定控件
asp.net c#自定义控件 代码例子
一款自定义的控件,通过操作txt,html格式的文件达到repeater控件的效果,在CSS使用方面比repeater控件更易于操作,输出到页面后完全是html的标签,如果想在输出过程中加入服务器控件的话,改案例还无法达到要求,...
超强的ASP.NET自定义客户端验证控件(推荐)
一段ASP.NET自定义控件源代码,里面是一个简单的自定义控件,包含源代码.
asp.net 自定义用户控件 事件,需要的下吧。
ASP.NET自定义控件实例教程.pdf
一些asp.net的控件开发的实例。 相对比较简单的自定义控件。
ASP.NET自定义控件教程.pdf
ASP.NET自定义控件 真正可以评分的星级控件,实现动态评分的效果
ASP.NET 自定义菜单MENU控件,直接添加引用DLL,即可使用,美观,自定义菜单样式图片,也可以替换,随意使用,附件是调用的DEMO,分享给大家。
asp.net 自定义控件 CalendarTextBox