ASP.NET Design Control Purification Website Language (3)

zhaozj2021-02-16  51

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 ("

Please enter the text below:")) 'Text input box DIM Box1 as new textbox () Box1.text = "" Controls.add (Box1) Controls.add (New LitralControl (")) 'button Dim Button1 As New Button () Button1.Text =" Submit "Controls.Add (New LitralControl ("
") Controls.add (Button1) 'Add an event handle to the newly created button object AddHandler Button1.Click, addressof me.buttonclickedControls.Add (New Litralcontrol ("
")) label = new label () label .Height = unit.pixel (50) label.width = unit.pixel (500) label.text = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" .Text = "" End Subprivate Sub ButtonClicked (Sender AS [Object], E AS Eventargs) oncheck (New Ch EcKeventargs (CType (CONTROLS (1), TextBox) .Text, Checkstring (CTYPE (CONTROLS (1), TextBox) .Text)) The main task of the above code is: (1) First import the necessary namespace, declaration The namespace belonging to the current class. (2) Next, define the main body of Composite. Composite is inherited from the most basic Control class and also implements the InamingContainer interface. The INAMINGCONTAINER interface allows the Composite control to forward events to its Button sub-control. (3) Creating a child control with a CreateChildControls method (not an onInit or constructor). ⑷ Composite control does not show the Click event of the Button subfeit. Instead, it handles the Click event and throws a custom event Check.

⑸ 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

转载请注明原文地址:https://www.9cbs.com/read-21091.html

New Post(0)