Development of composite controls

Source: Internet
Author: User


Composite Control
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Using system. Collections. Specialized;
Namespace custom
{
[Defaultproperty ("text "),
Toolboxdata ("<{0}: rendered runat = Server> </{0}: rendered>")]
Public class rendered: control, ipostbackdatahandler, ipostbackeventhandler
{
Private string text1;
Private string text2;
Private string text = "click the submit button to check whether the request matches .";
Private int number = 100;
Private int sum
{
Get
{
Return int32.parse (text1) +
Int32.parse (text2 );
}
}
Public int number
{
Get
{
Return number;
}
Set
{
Number = value;
}
}
Public String text
{
Get
{
Return text;
}
Set
{
TEXT = value;
}
}
Public event checkeventhandler check;

Protected virtual void oncheck (checkeventargs CE)
{
If (check! = NULL)
{
Check (this, CE );
}
}
Public Virtual bool loadpostdata (string postdatakey,
Namevaluecollection values)
{
Text1 = values [uniqueid + "T1"];
Text2 = values [uniqueid + "T2"];
Page. registerrequiresraiseevent (this );
Return false;
}
Public Virtual void raisepostdatachangedevent ()
{
}
Public void raisepostbackevent (string eventargument)
{
Oncheck (New checkeventargs (Sum-number ));
}
Protected override void render (htmltextwriter output)
{
Output. Write ("Output. Write ("<input type = text name =" + this. uniqueid + "T1" +
"Value = '0'> ");
Output. Write ("Output. Write ("<br> Output. Write ("<input type = text name =" + this. uniqueid + "T2" +
"Value = '0'> ");
Output. Write ("Output. Write ("<br> <input type = submit name =" +
This. uniqueid + "value = 'Submit'> ");
Output. Write ("<br> <span style = 'height: 50px; width: 500px; '>"
+ TEXT + "</span> ");
}
}
Public class checkeventargs: eventargs
{
Private bool match = false;

Public checkeventargs (INT difference)
{
If (difference = 0)
{
Match = true;
}
}
Public bool match
{
Get
{
Return match;
}
}
}
Public Delegate void checkeventhandler (Object sender, checkeventargs CE );
}
Public void sum_checked (Object sender, customcontrols. checkeventargs E)
{
If (E. Match = true)
Composite1.text = "<H2> your input matches! </H2> ";
Else
Composite1.text = "sorry, the sum of the numbers you entered is not equal to the built-in number! ";
}
User Controls and composite controls
Myusercontrol. ascx
Using system;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace compositecontrol
{
Public abstract class myusercontrol: system. Web. UI. usercontrol
{
Protected system. Web. UI. webcontrols. textbox box1;
Protected system. Web. UI. webcontrols. textbox box2;
Protected system. Web. UI. webcontrols. Button button;
Protected system. Web. UI. webcontrols. Label label;
Private int sum
{
Get
{
Ensurechildcontrols ();
Return int32.parse (box1.text) +
Int32.parse (box2.text );
}
}
Public int number = 100;
Public String text
{
Get
{
Ensurechildcontrols ();
Return label. text;
}
Set
{
Ensurechildcontrols ();
Label. Text = value;
}
}
Public event checkeventhandler check;
Public Virtual void oncheck (checkeventargs E)
{
If (check! = NULL)
{
Check (this, e );
}
}
Public void button_clicked (Object sender, eventargs E)
{
Oncheck (New checkeventargs (Sum-number ));
}
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}
}
Public class checkeventargs: eventargs
{
Private bool match = false;

Public checkeventargs (INT difference)
{
If (difference = 0)
{
Match = true;
}
}
Public bool match
{
Get
{
Return match;
}
}
}
Public Delegate void checkeventhandler (Object sender, checkeventargs CE );
}

Public void sum_checked (Object sender, checkeventargs E)
{
If (E. Match = true)
Myusercontrol1.text = "<H2> your input matches! </H2> ";
Else
Myusercontrol1.text = "sorry, the sum of the numbers you entered is not equal to the built-in number! ";
}

Use Custom Controls
Mycomposite. CS
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Namespace customcontrols
{
/// <Summary>
/// Summary of webcustomcontrol1.
/// </Summary>
[Defaultproperty ("text "),
Toolboxdata ("<{0}: Composite runat = Server> </{0}: Composite>")]
Public class composite: control, inamingcontainer
// Identifies the container control that creates the new ID namespace in the control hierarchy of the page object. This is only a tag interface.
{
Private int number = 100;
Private Label label;

Public int number
{
Get
{
Return number;
}
Set
{
Number = value;
}
}
Private int sum
{
Get
{
Ensurechildcontrols ();
Return int32.parse (textbox) controls [1]). Text) +
Int32.parse (textbox) controls [4]). Text );
}
}
Public String text
{
Get
{
Ensurechildcontrols ();
Return label. text;
}
Set
{
Ensurechildcontrols ();
Label. Text = value;
}
}
Public event checkeventhandler check;
Protected virtual void oncheck (checkeventargs CE)
{
If (check! = NULL)
{
Check (this, CE );
}
}
Protected override void createchildcontrols ()
{
Controls. Add (New literalcontrol ("Textbox box1 = new Textbox ();
Box1.text = "0 ";
Controls. Add (box1 );
Controls. Add (New literalcontrol ("Controls. Add (New literalcontrol ("Textbox box2 = new Textbox ();
Box2.text = "0 ";
Controls. Add (box2 );
Controls. Add (New literalcontrol ("Button button1 = new button ();
Button1.text = "Submit ";
Controls. Add (New literalcontrol ("<br> "));
Controls. Add (button1 );
Button1.click + = new eventhandler (this. buttonclicked );
Controls. Add (New literalcontrol ("<br> "));
Label = new label ();
Label. Height = 50;
Label. width = 500;
Label. Text = "click the submit button to see if the button matches ";
Controls. Add (Label );
}
Protected override void onprerender (eventargs E)
{
(Textbox) controls [1]). Text = "0 ";
(Textbox) controls [4]). Text = "0 ";
}
Private void buttonclicked (Object sender, eventargs E)
{
Oncheck (New checkeventargs (Sum-number ));
}
}
Public class checkeventargs: eventargs
{
Private bool match = false;

Public checkeventargs (INT difference)
{
If (difference = 0)
{
Match = true;
}
}
Public bool match
{
Get
{
Return match;
}
}
}
Public Delegate void checkeventhandler (Object sender, checkeventargs CE );
}

Public void sum_checked (Object sender, custom. checkeventargs E)
{
If (E. Match = true)
Rendered. Text = "<H2> match you entered! </H2> ";
Else
Rendered. Text = "sorry, the sum of the numbers you entered is not equal to the built-in number! ";
}


Event Processing for composite controls
Eventbubbler. CS
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Namespace compositeevent
{
[Defaultproperty ("text "),
Toolboxdata ("<{0}: eventbubbler runat = Server> </{0}: eventbubbler>")]
Public class eventbubbler: control, inamingcontainer
{
Private int number = 100;
Private Label label;
Private textbox box1;
Private textbox box2;
Public event eventhandler click;
Public event eventhandler reset;
Public event eventhandler submit;
Public String label
{
Get
{
Ensurechildcontrols ();
Return label. text;
}
Set
{
Ensurechildcontrols ();
Label. Text = value;
}
}
Public int number
{
Get
{
Return number;
}
Set
{
Number = value;
}
}
Public String text1
{
Get
{
Ensurechildcontrols ();
Return box1.text;
}
Set
{
Ensurechildcontrols ();
Box1.text = value;
}
}
Public String text2
{
Get
{
Ensurechildcontrols ();
Return box2.text;
}
Set
{
Ensurechildcontrols ();
Box2.text = value;
}
}
Protected override void createchildcontrols ()
{
Controls. Add (New literalcontrol ("Box1 = new Textbox ();
Box1.text = "0 ";
Controls. Add (box1 );
Controls. Add (New literalcontrol ("Controls. Add (New literalcontrol ("Box2 = new Textbox ();
Box2.text = "0 ";
Controls. Add (box2 );
Controls. Add (New literalcontrol ("Button button1 = new button ();
Button1.text = "click ";
Button1.commandname = "click ";
Controls. Add (button1 );
Button button2 = new button ();
Button2.text = "reset ";
Button2.commandname = "reset ";
Controls. Add (button2 );
Button button3 = new button ();
Button3.text = "Submit ";
Button3.commandname = "Submit ";
Controls. Add (button3 );
Controls. Add (New literalcontrol ("<br> "));
Label = new label ();
Label. Height = 50;
Label. width = 500;
Label. Text = "click a button ";
Controls. Add (Label );
}
Protected override bool onbubbleevent (Object source, eventargs E)
{
Bool handled = false;
If (E is commandeventargs)
{
Commandeventargs Ce = (commandeventargs) E;
If (Ce. commandname = "click ")
{
Onclick (CE );
Handled = true;
}
Else if (Ce. commandname = "reset ")
{
Onreset (CE );
Handled = true;
}
Else if (Ce. commandname = "Submit ")
{
Onsubmit (CE );
Handled = true;
}
}
Return handled;
}
Protected virtual void onclick (eventargs E)
{
If (Click! = NULL)
{
Click (this, e );
}
}
Protected virtual void onreset (eventargs E)
{
If (reset! = NULL)
{
Reset (this, e );
}
}
Protected virtual void onsubmit (eventargs E)
{
If (submit! = NULL)
{
Submit (this, e );
}
}
}
}

Public void clickhandler (Object sender, eventargs E)
{
Mycontrol. Label = "you have clicked <B> click </B> ";
}
Public void resethandler (Object sender, eventargs E)
{
Mycontrol. text1 = "0 ";
Mycontrol. text2 = "0 ";
}
Public void submithandler (Object sender, eventargs E)
{
If (int32.parse (mycontrol. text1) + int32.parse (mycontrol. text2) = mycontrol. Number)
Mycontrol. Label = "<H2> matched! </H2> ";
Else
Mycontrol. Label = "sorry, the sum of the numbers you entered is not equal to the built-in number! ";
}


Compound Control that limits input content
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Using system. xml;
Using system. collections;
Namespace compositeinput
{
[Defaultproperty ("text "),
Toolboxdata ("<{0}: cominput runat = Server> </{0}: cominput>")]
Public class cominput: control, inamingcontainer
// Identifies the container control that creates the new ID namespace in the control hierarchy of the page object. This is only a tag interface.
{
Private string strfilename = "bad_words.xml ";
Private Label label;
Public String filename
{
Get
{
Return strfilename;
}
Set
{
Strfilename = value;
}
}
// 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 String checkstring (string inputstring)
{
Arraylist alwordlist = new arraylist ();
String xmldocpath = This. Page. server. mappath ("bad_words.xml ");
Xmltextreader xmlreader = new xmltextreader (xmldocpath );
String asterisks = "*************************";
// Read the XML file that defines offensive words into an arraylist
While (xmlreader. Read ())
{
If (xmlreader. nodetype = xmlnodetype. Text)
Alwordlist. Add (xmlreader. value );
}
Xmlreader. Close ();
// Check the text content submitted by the user and replace the offensive words with an appropriate number of asterisks
Foreach (string element in alwordlist)
Inputstring = inputstring. Replace (element, asterisks. substring (1, (element. Length )));
Return inputstring;
}
Public String text
{
Get
{
Ensurechildcontrols ();
Return label. text;
}
Set
{
Ensurechildcontrols ();
Label. Text = value;
}
}
Public event checkeventhandler check;
Protected virtual void oncheck (checkeventargs CE)
{
If (check! = NULL)
{
Check (this, CE );
}
}
Protected override void createchildcontrols ()
{
Controls. Add (New literalcontrol ("Textbox box1 = new Textbox ();
Box1.text = "";
Controls. Add (box1 );
Controls. Add (New literalcontrol ("Button button1 = new button ();
Button1.text = "Submit ";
Controls. Add (New literalcontrol ("<br> "));
Controls. Add (button1 );
Button1.click + = new eventhandler (this. buttonclicked );
Controls. Add (New literalcontrol ("<br> "));
Label = new label ();
Label. Height = 50;
Label. width = 500;
Label. Text = "";
Controls. Add (Label );
}
Protected override void onprerender (eventargs E)
{
(Textbox) controls [1]). Text = "";
}
Private void buttonclicked (Object sender, eventargs E)
{
Oncheck (New checkeventargs (textbox) controls [1]). Text, checkstring (textbox) controls [1]). Text )));
}
}
Public class checkeventargs: eventargs
{
Private bool match = false;

Public checkeventargs (string str1, string str2)
{
If (str1 = str2)
{
Match = true;
}
}
Public bool match
{
Get
{
Return match;
}
}
}
Public Delegate void checkeventhandler (Object sender, checkeventargs CE );
}

Public void checktext (Object sender, compositeinput. checkeventargs E)
{
If (E. Match)
Cominput1.text = "Your submitted content has passed the check! ";
Else
Cominput1.text = "<H2> the content must comply with the site rules! Do not publish offensive words! </H2> ";
}

 

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.