Now look at the composite control itself. Composite controls have two classes, implemented with two separate VB source files, respectively, respectively, respectively. VB and Checkeevent.vb. [Composite.vb]
Imports SystemImports System.WebImports System.Web.UIImports System.Web.UI.WebControlsImports System.XmlImports System.CollectionsNamespace CustomControlsPublic Class CompositeInherits ControlImplements INamingContainerPrivate _filename As String = "bad_words.xml" Private label As LabelPrivate box1 As TextBoxPublic Property filename () As StringGetReturn _FileNamend GetSet_FileName = ValueEnd Setnd Property 'The text content submitted by the user is the input parameter. If the content submitted by the user contains an attack word, 'returns the modified version,' Otherwise, returns the original text directly. Public Function CheckString (InputString as String) as stringDim alWordList As new ArrayListdim xmlDocPath as string = mappathsecure ( "bad_words.xml") dim xmlReader as XmlTextreader = new xmlTextReader (xmlDocPath) dim element as stringdim output as stringdim asterisks as string = "** *********************** "" will read the contents of the XML file that defines the attack-style XML file into an arraylistWhile (XmlReader.Read ()) if XmlReader.Nodetype = xmlNodeType.Text thenalWordList.Add (xmlReader.Value) end ifend whilexmlReader.Close () 'check text submitted by the user, will replace offensive words Each element is the appropriate number of asterisks For in alWordListInputString = InputString.Replace (element, Asterisks.Substring (1, (Element.Length)) Next Return InputString End () AS STRINGET 'First check the current value of the childControlScreated property.
If the value is false, 'CreateChildControls method is called EnsureChildControls () Return label.TextEnd GetSetEnsureChildControls () label.Text = valueEnd SetEnd PropertyPublic Event Check As CheckEventHandlerProtected 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 ("
⑸ Composite control reveals the following common attributes: Text, namely the text attribute value of the label sub-control; FileName, the name of the XML file that allows and setting the XML file that defines the attack word, the main check function is implemented by the checkstring method, its input parameters are A text string. Checkstring method Reads disabled words from an XML file, puts an arrayList, and checks if the specified string contains disabled words. All "Attack" words will be replaced by the appropriate number "*". ⑺ On OnPrender Clears the text of the text box control. ⑻ When the user clicks the button, ButtonClicked starts. ButtonClicked calls the oncheck sub-process, incoming appropriate parameters (a new Checkeentargs object, the parameter for creating the CheckeventArgs object is the text before and after the check). OnCheck then triggers an event, which will be processed by the Code in the .aspx page. ] [CheckEvent.vb event handler 'custom event data class contains code CheckEventArgs.' Check also defined event Imports SystemNamespace CustomControlsPublic Class CheckEventArgsInherits EventArgsPrivate _match As Boolean = FalsePublic Sub New (string1 As String, string2 as String) If string1 = string2 Then_match = TrueEnd IfEnd SubPublic ReadOnly Property match () As BooleanGetReturn _matchEnd getEnd PropertyEnd ClassPublic Delegate Sub CheckEventHandler (sender As Object, ce As CheckEventArgs) End Namespace CheckEventArgs constructor two strings are arranged according to the corresponding value of the string matching Tag_match. In addition, the above code also defines the CHECKEVENTHANDLER event handle. After writing the above code, if you don't install the IDE, use the following command to perform compiled:
VBRARY /OUT :./bin/custom controls.dll /r:system.dll /r:System.Dawing.dll / r: system.data.dll / r: system.data.dll / r: System. Xml.dll * .vb
Previous 1 2 3