`

自定义asp.net控件分析

阅读更多

下面就以,.net自动生成的模版做一解释。(以vb语言为例)<!----><o:p></o:p>

1.Imports System.ComponentModel<o:p></o:p>

2.Imports System.Web.UI<o:p></o:p>

3.<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> Public Class WebCustomControl1<o:p></o:p>

4.    Inherits System.Web.UI.WebControls.WebControl<o:p></o:p>

5.    Dim _text As String<o:p></o:p>

6.    <Bindable(True), Category("str"), DefaultValue("11111")> Property [Text]() As String<o:p></o:p>

7.         Get<o:p></o:p>

8.            Return _text<o:p></o:p>

9.         End Get<o:p></o:p>

10.        Set(ByVal Value As String)<o:p></o:p>

11.            _text = Value<o:p></o:p>

12.        End Set<o:p></o:p>

13.    End Property<o:p></o:p>

14.    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)<o:p></o:p>

15.        output.Write([Text])<o:p></o:p>

16.    End Sub<o:p></o:p>

17.End Class<o:p></o:p>

'---------------------------------------------------------------<o:p></o:p>

'1-2 导入命名空间,System.ComponentModel和 System.Web.UI 这没什么好介绍的<o:p></o:p>

'3 DefaultProperty("Text")--指定属性的默认值。如果用此属性需要导入(命名空间: System.ComponentModel)<o:p></o:p>

ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")<o:p></o:p>

指定当从 Visual Studio 等工具中的工具箱拖动自定义控件时为它生成的默认标记。<o:p></o:p>

在下面的示例中,设置特定于 MyLabel 的若干属性。{0} 的所有匹配项都由设计器替换为与 MyLabel 类关联的标记前缀。<o:p></o:p>

<ToolboxData("<{0}:MyLabel Text='MyLabel' BorderColor='Yellow' BackColor='Magenta' BorderWidth = '10'  runat='server'></{0}:MyLabel>")><o:p></o:p>

Public Class WebCustomControl1定义类名为webcustomcontrol1,以后编译生成的dll名为webcustomtrol1<o:p></o:p>

(注意:如果你修改类名。则需要修改{0}:后相对应的名字。例如:你把类名webcustomcontrol1改为webcustom。<o:p></o:p>

则需要把ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")改成<o:p></o:p>

ToolboxData("<{0}:webcustom runat=server></{0}:webcustom>") 否则编译后将出错。)<o:p></o:p>

'4 Inherits 表示继承。这里是继承System.Web.UI.WebControls.WebControl的方法,属性,事件等。<o:p></o:p>

'6 这句主要是控制自定义控件在’属性浏览器‘中的显示,先解释模版的句子,再扩展开讲<o:p></o:p>

Property [Text]() As String定义 text属性 为字符串类型<o:p></o:p>

Bindable(True)指定是否要绑定到该属性。-True为是,False为不<o:p></o:p>

Category("Appearance") --text属性将显示在外观组中。指定类别的名称,在该类别中将对属性或事件进行分组。当使用了类别时,组件属性和事件可以按逻辑分组显示在属性浏览器中。<o:p></o:p>

DefaultValue("")为属性设置一个简单的默认值。这里为空<o:p></o:p>

下面列出所有的特性<o:p></o:p>

详细资料可查看ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpcondesign-timeattributesforcomponents.htm<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

属性<o:p></o:p>

应用于<o:p></o:p>

说明<o:p></o:p>

BrowsableAttribute<o:p></o:p>

属性和事件<o:p></o:p>

指定属性或事件是否应该显示在属性浏览器中。<o:p></o:p>

CategoryAttribute<o:p></o:p>

属性和事件<o:p></o:p>

指定类别的名称,在该类别中将对属性或事件进行分组。当使用了类别时,组件属性和事件可以按逻辑分组显示在属性浏览器中。<o:p></o:p>

DescriptionAttribute<o:p></o:p>

属性和事件<o:p></o:p>

定义一小块文本,该文本将在用户选择属性或事件时显示在属性浏览器底部。<o:p></o:p>

BindableAttribute<o:p></o:p>

属性<o:p></o:p>

指定是否要绑定到该属性。<o:p></o:p>

DefaultPropertyAttribute<o:p></o:p>

属性

(将此特性插入类声明前。)

指定组件的默认属性。当用户单击控件时,将在属性浏览器中选定该属性。<o:p></o:p>

DefaultValueAttribute<o:p></o:p>

属性<o:p></o:p>

为属性设置一个简单的默认值。<o:p></o:p>

EditorAttribute <o:p></o:p>

属性<o:p></o:p>

指定在可视设计器中编辑(更改)属性时要使用的编辑器。<o:p></o:p>

LocalizableAttribute<o:p></o:p>

属性<o:p></o:p>

指定属性应本地化。当用户要本地化某个窗体时,任何具有该特性的属性都将自动永久驻留到资源文件中。<o:p></o:p>

DesignerSerializationVisibilityAttribute <o:p></o:p>

属性<o:p></o:p>

指定显示在属性浏览器中的属性是否应该(以及如何)永久驻留在代码中。<o:p></o:p>

TypeConverterAttribute <o:p></o:p>

属性<o:p></o:p>

指定将属性的类型转换为另一个数据类型时要使用的类型转换器。<o:p></o:p>

DefaultEventAttribute<o:p></o:p>

事件

(将此特性插入类声明前。)

指定组件的默认事件。这是当用户单击组件时在属性浏览器中选定的事件。<o:p></o:p>

 <o:p></o:p>

.net中还支持自定义特性,这里就不说,有兴趣的可以去查msdn,上面有详细说明  <o:p></o:p>

可参考ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconwritingcustomattributes.htm<o:p></o:p>

7-12很简单,意思就是返回(Get)Text属性的值和设置(Set)Text属性的值<o:p></o:p>

13 Text属性的结束<o:p></o:p>

14-16 这个过程作用是重写控件的呈现。这里是在页面上显示Text属性的值<o:p></o:p>

 <o:p></o:p>

asp.net中当你想对buttonclick事件做确认操作,但Button按钮不能满足此要求。就针对此要求来编写自己的控件。

======================================================================

继承:System.Web.UI.WebControls.Button

控件功能:弹出确认消息框

控件属性:message(消息框中显示的信息)

控件方法:不需要

控件事件:不需要

使用方法:“确定”执行按钮的button_click事件,“取消”不执行任何事件。

Imports System.ComponentModel<o:p></o:p>

Imports System.Web.UI<o:p></o:p>

 <o:p></o:p>

Namespace WebControls<o:p></o:p>

 <o:p></o:p>

    <DefaultProperty("Text"), ToolboxData("<{0}:ConfirmButton runat=server></{0}:ConfirmButton>")> Public Class ConfirmButton<o:p></o:p>

         '继承button<o:p></o:p>

        Inherits System.Web.UI.WebControls.Button<o:p></o:p>

        '为其所包含的任何服务器控件提供唯一的命名空间<o:p></o:p>

        Implements INamingContainer<o:p></o:p>

        Dim _Message As String<o:p></o:p>

        '定义message属性。<o:p></o:p>

        <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Message]() As String<o:p></o:p>

            Get<o:p></o:p>

                Return _Message<o:p></o:p>

            End Get<o:p></o:p>

            Set(ByVal Value As String)<o:p></o:p>

                _Message = Value<o:p></o:p>

            End Set<o:p></o:p>

        End Property<o:p></o:p>

 <o:p></o:p>

        Public Sub New()<o:p></o:p>

            _Message = ""<o:p></o:p>

        End Sub<o:p></o:p>

'重写控件的输出<o:p></o:p>

        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)<o:p></o:p>

           '为控件增加客户端onclick事件。<o:p></o:p>

            If Me.Message.Trim <> "" Then Me.Attributes.Add("onClick", "jscript:if(!confirm('" & Me.Message & "')) return false;")<o:p></o:p>

            Me.Attributes.Add("onFocus", "jscript:this.blur();")<o:p></o:p>

            MyBase.Render(output)<o:p></o:p>

        End Sub<o:p></o:p>

    End Class<o:p></o:p>

End Namespace<o:p></o:p>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics