ASP. NET design controls purify Website Languages (3)

Source: Internet
Author: User
Now let's look at the composite control itself. Composite controls are implemented using two independent VB source files: Composite. VB and checkevent. VB.

[Composite. VB]

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

'Use the text content submitted by the user as the input parameter. If the content submitted by the user contains offensive words,
', The modified version is returned,
'Otherwise, the original text is directly returned.
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 XML file that defines offensive words into an 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 attribute. If the value is false,
'Then the createchildcontrols method is called.
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 (New literalcontrol ("

'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> "))
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:

(1) first, import the necessary namespace and declare the namespace to which the current class belongs.

(2) define the subject of composite. Composite inherits 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 subcontrol.

(3) Use the createchildcontrols method (instead of oninit or constructor) to create a child control.

(4) The composite control does not display the Click Event of the button subcontrol. Instead, it processes the Click Event and throws the Custom Event check.

The custom Composite Control exposes the following common attributes: text, that is, the text attribute value of the label sub-control; filename, which allows you to obtain and set the name of an XML file that defines an aggressive word.

The main checking function of the callback is implemented by the checkstring method. Its input parameter is a text string. The checkstring method reads the disabled words from the XML file, puts them into an array list (arraylist), and checks whether the specified string contains the disabled words. All "aggressive" words will be replaced by an appropriate number.

Deleonprerender clears the text of the text box subcontrol.

When the user clicks the button, buttonclicked starts execution. Buttonclicked calls the oncheck sub-process and passes in appropriate parameters (a newly created checkeventargs object, and the parameters for creating the checkeventargs object are pre-check and post-check texts ). Oncheck then triggers an event, which is processed by the Code on the. ASPX page.

[Checkevent. VB]

'The code that contains the Custom Event Data class checkeventargs.
'Also defines the event handle of the 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 constructor of checkeventargs is two strings, and the matching tag _ match is set based on the value of the string. In addition, the Code above defines the checkeventhandler event handle.

After writing the above Code, if you have not installed the IDE, run the following command to compile it:

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

Previous Page 1 2 3

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.