您的位置:首页 >> Web开发 >> ASP.NET >> ASP.Net教程 >> 正文
ASP.Net教程 RSS
 

自定义asp.net控件分析(二)

http://www.rdxx.com 06年12月13日 23:36 互联网 我要投稿

关键词: ASP.NET控件
 

自定义asp.net控件分析(二)


上一篇分析了自定义控件的基本语法。这次编写一控件来作为实例。

asp.net中当你想对buttonclick事件做确认操作,但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控件 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站