自定义asp.net控件分析
2004-08-17 11:09 无常 阅读(777) 评论(0) 编辑 收藏 举报自定义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 |
属性 |
指定属性应本地化。当用户要本地化某个窗体时,任何具有该特性的属性都将自动永久驻留到资源文件中。 |
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控件分析(二)
上一篇分析了自定义控件的基本语法。这次编写一控件来作为实例。
在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
到此,控件就编写完了,你看是不是很简单。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架