Consider this scenario: you write an application for a Web site that accepts user input and permanently saves the input, such as saving it to a database and displaying the user input on the site, such as a forum is a typical example.
If the user is a source of clutter, you must consider how to prevent users from submitting and posting offensive (or pornographic, vulgar) content. Possible solutions include:
⑴ restricts the user base to a closed user community, which requires users to register/log in before they can use program features. In this way, the likelihood of users breaking the site's rules is much smaller because each submission can be traced to the submitter. If a user does something that should not be done, you can verify the identity of the user and deal with it accordingly.
⑵ is reviewed by the administrator before publishing the content submitted by the user on the Web site. In many cases, because of limited human resources, this approach does not necessarily work.
⑶ prohibits users from submitting offensive content. This is the ideal solution to solve the problem in its origins. But how exactly should it be achieved?
The scenario described in this article is based on a composite control that uses an XML file to define offensive words. We'll use vb.net to write this composite control and complete the project with a normal text editor and command line compiler (VBC).
Before you formally write the control, let's briefly review the concept of the control in ASP.net. All the controls that appear in this article are server controls that run on the server and send HTML code to the client. To understand the classification of controls, you can view them from the perspective of whether the control is embedded in a Web Form page (and thus on an on-demand basis) or precompiled. Microsoft defines the following ASP.net server controls: HTML server controls, Web server controls, validation controls, user controls.
The first three types of control readers should have been familiar with, for developers, they are the simplest type of control, in the ASP.net has been written by Microsoft for us. User controls are different. The user control is an. aspx page in the form of "wrapper" into an. ascx page, and other. aspx pages can invoke the functionality of the user control by registering and instantiating it. This is a high expectation of the server-side control, for asp/asp. NET developer, it represents a major step forward, and, in particular, the language in which the controls are written now fully supports object-oriented technology.
Asp. NET user controls consist of one or more server controls, static HTML elements, and can contain additional code, each encapsulating a specific set of features. User controls can be obtained by simply extending an existing server control (a group of controls), such as a graphics control with rotation, and a calendar control that saves dates in a text box.
Second, the development of composite controls
Control to check whether the content submitted by the user contains "offensive" words, the offensive words are defined by an XML file, and the XML file is structured as follows:
<?xml version= "1.0"? Encoding= "GB2312" >
<words>
<word> Word One </word>
<word> Word two </word>
</words>
The composite Control (composite) in this article contains three ASP.net server controls: A TextBox control, a Label control, and a button control. When the user clicks on the button control, composite checks whether the user submitted text contains the words specified in the XML file (the default name for the XML file is bad_words.xml, defined by a custom attribute), and throws a custom event. In addition, the composite control exposes a text property of its label child control to the top-level property.
A composite control can selectively expose child controls as properties or selectively expose the properties and events of child controls as top-level properties and events. When a composite control consolidates a property from a child control, it typically simply delegates the child control to the action, as shown in the following example:
Delegates the action to the label object, which is a
Examples of 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
We need a text entry box for the user to enter the content, a button to submit the form, and a text label to give feedback to the user. Here's a look at the code for the Web form, where the composite control is instantiated:
"Composite.aspx"
<%@ page language= "vb" debug= "false" Trace= "false"%>
<%@ Register tagprefix= "Custom" namespace= "customcontrols" Assembly = "CustomControls"%>
<script language= "VB" runat=server>
Private Sub Checktext (sender as Object, E as Checkeventargs)
If E.match = False Then
Composite.text = "Else
Composite.text = "You have submitted the content has passed the check!" "
End If
End Sub
</script>
<body>
Language Purification Control Instance <br>
<form runat=server>
<custom:composite id = "Composite" Oncheck = "Checktext"
filename = "Bad_words.xml" runat = server/> </form> </body>
The code above registers the specified composite control first. We will compile the control code into a. dll file and put it into the application Bin directory, which is where ASP.net first searches. In the user interface for Web Forms, we instantiate the custom control and specify:
⑴ executes a local child procedure Checktext when the control throws the Oncheck event. We expose the text of the label of the composite control through a common property, and the contents of the label are determined by another common property set by the Oncheck event handle.
⑵ the name of an XML file that defines an offensive word.
⑶ In addition, we have defined a checktext procedure called by a composite control.
Now look at the composite control itself. The composite control has two classes and is implemented with two separate VB source files, namely Composite.vb and Checkevent.vb.
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
' The text content submitted by the user is an input parameter. If the user submits a content that contains offensive rhetoric,
' Then return the modified version,
' Otherwise, return the original text directly.
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 = "*************************"
' Read the content of the XML file that defines the offensive rhetoric into a ArrayList
while (Xmlreader.read ())
If Xmlreader.nodetype=xmlnodetype.text Then
Alwordlist.add (Xmlreader.value)
End If
End While
Xmlreader.close ()
' Check the text content submitted by the user and replace the offensive words with an appropriate number of asterisks
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
' This method first checks the current value of the ChildControlsCreated property. If the value is false,
' Then call the CreateChildControls method
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
' Create a child control of the composite control
Protected Overrides Sub CreateChildControls ()
Controls.Add ("
' Text input Box
Dim Box1 as New Textbox ()
Box1. Text = ""
Controls.Add (Box1)
Controls.Add (New LiteralControl ("
' button
Dim button1 as New Button ()
Button1. Text = "Submit"
Controls.Add (New LiteralControl ("<br>"))
Controls.Add (button1)
' Add an event handle to the newly created button object
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
The main tasks of the code above are:
⑴ first imports the necessary namespaces and declares the namespace to which the current class belongs.
⑵ next defines the body of the composite. Composite inherits from the most basic control class and implements the INamingContainer interface. The INamingContainer interface allows the composite control to forward events to its button child control.
⑶ creates child controls using the CreateChildControls method, rather than the OnInit or constructor.
The ⑷composite control does not expose the Click event of the button child control. Instead, it handles the Click event and throws the custom event check.
The ⑸composite control exposes the following common properties: text, which is the value of the Text property of the label child control; FileName, which allows you to get and set the name of an XML file that defines offensive words
⑹ The main check function is implemented by the Checkstring method, and its input parameter is a text string. The Checkstring method reads a disabled word from an XML file, puts it in an array list (ArrayList), and then checks whether the specified string contains a disabled word. All "offensive" words will be replaced by an appropriate number of "*".
⑺onprerender clears the text of the text box control.
⑻ when the user clicks on the button, ButtonClicked begins to execute. ButtonClicked calls the Oncheck procedure, passing in the appropriate arguments (a newly created Checkeventargs object that creates the parameters for the Checkeventargs object before and after the check). Oncheck then triggers an event that will be handled by the code in the. aspx page.
"Checkevent.vb"
' Code that contains custom event data class Checkeventargs.
' also defines the event handle for a check event
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
The Checkeventargs constructor is a two string that sets the corresponding matching tag _match based on the value of the string. In addition, the above code also defines the Checkeventhandler event handle.
After writing the above code, if you do not have the IDE installed, execute the compilation with the following command:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.