一
考慮一下這種情形
如果用戶來源很雜
⑴ 將用戶群限制到一個封閉的用戶團體
⑵ 在網站上發布用戶提交的內容之前
⑶ 禁止用戶提交攻擊性內容
本文介紹的方案以一個復合控件為基礎
在正式編寫控件之前
前三種控件讀者應該已經比較熟悉了
ASP
二
控件要檢查用戶提交的內容是否包含
<?xml version=
<words>
<word>詞語一</word>
<word>詞語二</word>
</words>
本文的復合控件(Composite)包含三個ASP
復合控件可以有選擇地將子控件顯露成屬性
// 將操作委托給標簽對象
// System
Public Property Text() As String
Get
EnsureChildControls()
Return label
End Get
Set
EnsureChildControls()
label
End Set
End Property
我們需要一個文本輸入框讓用戶輸入內容
【composite
<%@ page language=
<%@ Register TagPrefix=
<html>
<script language=
Private Sub CheckText(sender As Object
If e
Composite
Else
Composite
End If
End Sub
</script>
<body>
<h
<form runat=server>
<Custom:Composite id =
filename =
</html>
上面的代碼首先注冊指定的復合控件
⑴ 當控件拋出OnCheck事件
⑵ 定義攻擊性詞語的XML文件的名字
⑶ 另外
現在來看復合控件本身
【composite
Imports System
Imports System
Imports System
Imports System
Imports System
Imports System
Namespace CustomControls
Public Class Composite
Inherits Control
Implements INamingContainer
Private _filename As String =
Private label As Label
Private box
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(
dim xmlReader as XmlTextreader = new xmlTextReader(xmlDocPath)
dim element as string
dim output as string
dim asterisks as string =
while (xmlReader
if xmlReader
alWordList
end if
end while
xmlReader
For Each element in alWordList
InputString=InputString
asterisks
Next
Return InputString
End Function
Public Property Text() As String
Get
EnsureChildControls()
Return label
End Get
Set
EnsureChildControls()
label
End Set
End Property
Public Event Check As CheckEventHandler
Protected Overridable Sub OnCheck(ce As CheckEventArgs)
RaiseEvent Check(Me
End Sub
Protected Overrides Sub CreateChildControls()
Controls
Dim box
box
Controls
Controls
Dim button
button
Controls
Controls
AddHandler button
Controls
label = New Label()
label
label
label
Controls
End Sub
Protected Overrides Sub OnPreRender(e As EventArgs)
CType(Controls(
End Sub
Private Sub ButtonClicked(sender As [Object]
OnCheck(New CheckEventArgs(CType(Controls(
CheckString(CType(Controls(
End Sub
End Class
End Namespace
上面代碼的主要任務是
⑴ 首先導入必要的名稱空間
⑵ 接下來定義Composite的主體
⑶ 用CreateChildControls方法(而不是OnInit或構造函數)創建子控件
⑷ Composite控件沒有顯露出Button子控件的Click事件
⑸ Composite控件顯露了下列公用屬性
⑹ 主要的檢查功能由CheckString方法實現
⑺ OnPreRender清除文本框子控件的文本
⑻ 當用戶點擊按鈕
【CheckEvent
Imports System
Namespace CustomControls
Public Class CheckEventArgs
Inherits EventArgs
Private _match As Boolean = False
Public Sub New(string
If string
_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
End Namespace
CheckEventArgs的構造函數是兩個字符串
編寫好上面的代碼後
vbc /t:library /out:
/r:System
From:http://tw.wingwit.com/Article/program/net/201311/15751.html