2. Develop composite controls
The control should check whether the content submitted by the user contains the "offensive" word. The offensive word is defined by an XML file. The structure of the XML file is as follows:
<? XML version = "1.0 "? Encoding = "gb2312">
<Words>
<Word> word 1 </word>
<Word> word 2 </word>
</Words> |
The composite control in this article contains three ASP. NET Server controls: A Textbox Control, a label control, and a button control. When you click the button control, composite checks whether the text submitted by the user contains the specified words in the XML file (the default name of the XML file is bad_words.xml, which is defined by a custom attribute ), and throw a custom event. In addition, the composite control exposes a text attribute of its label sub-control to a top-level attribute.
Composite controls can selectively expose child controls as properties, or selectively expose attributes and events of child controls as top-level properties and events. When a composite control integrates attributes from a sub-control, it simply commissions the sub-control to perform operations, as shown in the following example:
// Delegate the operation to the tag object. The tag object is
// System. Web. UI. webcontrols. Label instance
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 input box for the user to enter the content, a button to submit the form, and a text tag to feedback information to the user. Let's take a look at the web form code. The composite control is instantiated here:
[Composite. aspx]
<% @ Page Language = "VB" DEBUG = "false" trace = "false" %>
<% @ Register tagprefix = "Custom" namespace = "customcontrols" assembly = "customcontrols" %>
<HTML>
<Script language = "VB" runat = Server>
Private sub checktext (sender as object, e as checkeventargs)
If E. Match = false then
Composite. Text = "<H2> the published content must comply with the site rules! Do not publish offensive words! </H2>"
Else
Composite. Text = "the content you submitted has passed the check! "
End if
End sub
</SCRIPT>
<Body>
<H1> language purification control instance <Form runat = Server>
<Custom: Composite id = "composite" oncheck = "checktext"
Filename = "bad_words.xml" runat = server/> </form> </body>
</Html> |
The above code first registers the specified composite control. We will compile the control code into a. dll file and put it into the bin directory of the application. This is the first position for ASP. NET to search. In the user interface composed of web forms, we instantiate the custom control and specify:
(1) When the control throws an oncheck event, execute a local sub-process checktext. The label Text of the composite control is displayed through a public attribute. The label content is determined by another public attribute set by the oncheck event handle.
(2) Name of the XML file that defines offensive words.
(3) In addition, we also define a checktext sub-process called by the composite control.
Previous Page 1 2 3 next page