<?xml version="1.0"? encoding="GB2312"> <words> <word>词语一</word> <word>词语二</word> </words> |
// 将操作委托给标签对象,标签对象是一个 // System.Web.UI.WebControls.Label的实例 Public Property Text() As String Get EnsureChildControls() Return label.Text End Get Set EnsureChildControls() label.Text = value End Set End Property |
<%@ page language="vb" debug="false" trace="false" %> <%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %> <html> <script language="VB" runat=server> Private Sub CheckText(sender As Object, e As CheckEventArgs) If e.Match = false Then Composite.Text = "<h2>发布内容请遵守本站规则!不得发布攻击性言辞!</h2>" Else Composite.Text = "你提交的内容已通过检查!" End If End Sub </script> <body> <h1>语言净化控件实例</h1><br> <form runat=server> <Custom:Composite id = "Composite" OnCheck = "CheckText" filename = "bad_words.xml" runat = server/></form></body> </html> |
Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml Imports System.Collections Namespace CustomControls Public Class Composite Inherits Control Implements INamingContainer Private _filename As String = "bad_words.xml" Private label As Label Private box1 As TextBox Public Property filename() As String Get Return _filename End Get Set _filename = value End Set End Property @# 以用户提交的文本内容为输入参数。如果用户提交的内容包含攻击性言辞, @# 则返回修改后的版本, @# 否则,直接返回原始的文本。 Public Function CheckString(InputString as String) as string Dim alWordList As new ArrayList dim xmlDocPath as string = mappathsecure("bad_words.xml") dim xmlReader as XmlTextreader = new xmlTextReader(xmlDocPath) dim element as string dim output as string dim asterisks as string = "*************************" @# 将定义攻击性言辞的xml文件内容读入到一个ArrayList while (xmlReader.Read()) if xmlReader.NodeType=xmlNodeType.Text then alWordList.Add(xmlReader.Value) end if end while xmlReader.Close() @# 检查用户提交的文本内容,将攻击性言辞替换为适当数量的星号 For Each element in alWordList InputString=InputString.Replace(element, asterisks.substring(1, (element.length))) Next Return InputString End Function Public Property Text() As String Get @# 该方法首先检查ChildControlsCreated属性的当前值。如果该值是false, @# 则调用CreateChildControls方法 EnsureChildControls() Return label.Text End Get Set EnsureChildControls() label.Text = value End Set End Property Public Event Check As CheckEventHandler Protected Overridable Sub OnCheck(ce As CheckEventArgs) RaiseEvent Check(Me, ce) End Sub @#创建Composite控件的子控件 Protected Overrides Sub CreateChildControls() Controls.Add(New LiteralControl("<h3>请在下面输入文字内容: ")) @#文本输入框 Dim box1 As New Textbox() box1.Text = "" Controls.Add(box1) Controls.Add(New LiteralControl("</h3>")) @#按钮 Dim button1 As New Button() button1.Text = "提交" Controls.Add(New LiteralControl("<br>")) Controls.Add(button1) @# 将一个事件句柄加入新创建的按钮对象 AddHandler button1.Click, AddressOf Me.ButtonClicked Controls.Add(New LiteralControl("<br><br>")) label = New Label() label.Height = Unit.Pixel(50) label.Width = Unit.Pixel(500) label.Text = "" Controls.Add(label) End Sub Protected Overrides Sub OnPreRender(e As EventArgs) CType(Controls(1), TextBox).Text = "" End Sub Private Sub ButtonClicked(sender As [Object], e As EventArgs) OnCheck(New CheckEventArgs(CType(Controls(1), TextBox).Text, CheckString(CType(Controls(1), TextBox).Text))) End Sub End Class End Namespace |
@# 包含定制事件数据类CheckEventArgs的代码. @# 另外还定义了Check事件的事件句柄 Imports System Namespace CustomControls Public Class CheckEventArgs Inherits EventArgs Private _match As Boolean = False Public Sub New(string1 As String, string2 as String) If string1=string2 Then _match = True End If End Sub Public ReadOnly Property Match() As Boolean Get Return _match End Get End Property End Class Public Delegate Sub CheckEventHandler(sender As Object, ce As CheckEventArgs) End Namespace |
vbc /t:library /out:./bin/CustomControls.dll /r:System.dll /r:System.web.dll /r:System.drawing.dll /r:System.Data.dll /r:System.xml.dll *.vb |