** 自定义asp.net控件分析(二) ** ** **
上一篇分析了自定义控件的基本语法。这次编写一控件来作为实例。
在 asp.net 中当你想对 button 的 click 事件做确认操作,但 Button 按钮不能满足此要求。就针对此要求来编写自己的控件。
======================================================================
继承: System.Web.UI.WebControls.Button
控件功能:弹出确认消息框
控件属性: message (消息框中显示的信息)
控件方法:不需要
控件事件:不需要
使用方法:“确定”执行按钮的 button_click 事件,“取消”不执行任何事件。
Imports System.ComponentModel
Imports System.Web.UI
Namespace WebControls
1<defaultproperty("text"), runat="server" toolboxdata("<{0}:confirmbutton=""><!--{0}:ConfirmButton-->")> Public Class ConfirmButton
2
3'继承button
4
5Inherits System.Web.UI.WebControls.Button
6
7'为其所包含的任何服务器控件提供唯一的命名空间
8
9Implements INamingContainer
10
11Dim _Message As String
12
13'定义message属性。
14
15<bindable( ),="" category("appearance"),="" defaultvalue("")="" true=""> Property [Message]() As String
16
17Get
18
19Return _Message
20
21End Get
22
23Set ( ByVal Value As String )
24
25_Message = Value
26
27End Set
28
29End Property
30
31Public Sub New ()
32
33_Message = ""
34
35End Sub
36
37'重写控件的输出
38
39Protected Overrides Sub Render( ByVal output As System.Web.UI.HtmlTextWriter)
40
41'为控件增加客户端onclick事件。
42
43If Me .Message.Trim <> "" Then Me .Attributes.Add("onClick", "jscript:if(!confirm('" & Me .Message & "')) return false;")
44
45Me .Attributes.Add("onFocus", "jscript:this.blur();")
46
47MyBase .Render(output)
48
49End Sub
50
51End Class
52
53End Namespace
54
55到此,控件就编写完了,你看是不是很简单。</bindable(></defaultproperty("text"),>