1. What are components? Check msdn. Microsoft defines the component as follows. in the. NET Framework, the component refers to the implementation of the system. componentmodel. A class of the icomponent interface, or a class that is directly or indirectly derived from the class that implements the icomponent. This is a definition from the perspective of pure language (technology). In layman's terms, components are "software units that can be operated independently". Here we emphasize independent operations, this means that components must have low coupling and high reusability. Microsoft divides software into two parts: one is component, which refers to the unit with specific functions, independent operation, and no UI interface; the other is control, which is the control we often call, it refers to the UI interface units with specific functions and independent operation.
2. To learn the knowledge required by the Asp.net component, you can master either of them. NET language. We recommend that you use C # And C # as a brand new language. However, we also use the C ++ and Java syntaxes and introduce some new concepts, which are good for programmers. Understand the running mechanism of IIS and the running mode of Asp.net. Familiar with JavaScript. The powerful functions of this script language are excellent in processing client actions. Basically, all custom components are inseparable from Javascript. At the same time, CSS and DHTML are also well-known. No way. They rarely appear independently and always like group performances. 3. Do not ask questions about the difficulty of component design. You may have guessed a few points, one word: hard. You may notice that viewstate is rarely studied in depth when writing an Asp.net application. The reason is simple because the user object designed by viewstate itself is not an application programmer, it is a component designer. If it is not required by the client, you will not write a large number of JavaScript scripts in Asp.net, but it is difficult to escape the relationship in component design. More than that, are they designed as server components? Do our components inherit control, webcontrol, or component? Do I need to customize attributes in components? Do I need to bind data? How do I draw the component appearance? How to communicate with IIS? Do I need post-back? Many and many problems require component designers to consider them one by one. Therefore, if you care about it, isn't it just designing a component? How difficult is this! So, I will smile, because I know, you must be joking. But do not be afraid, "programmers need to explore the spirit !" 4. Select a base class. If we design a web visual control that forms a part of a Web page, we can inherit the control class or webcontrol class. If it is a non-visual control, it can inherit component. The control inherited from this class does not appear on the page but in component tray. Do you still remember the openfiledialog control? The open file dialog box control appears in the component tray control. If we only enhance functions on the basis of an existing control, inherit the existing control. 5. In practice, if we want to design a component that only allows users to enter numbers, the verification work should naturally be placed on the client. The client verification script can be written as follows: <HTML> Before rewriting the onprerender () method, write several constants: Private const string scp_number_only_script_id = "{signature}"; private const string scp_number_only_hook = "return every Y (this );"; private const string scp_number_only_script = "<script language =/" javascript1.2/">/nfunction using Y (CTRL)/n {/N" + "If (event. keycode = 13)/n return true;/n if (event. keycode <48 | event. keycode> 57)/n return false;/n else/n return true;/n }}" + "</SCRIPT> ";
The following method is used to verify code generation: Private void renderjavascript () {If (! Page. isclientscriptblockregistered (scp_number_only_script_id) page. registerclientscriptblock (scp_number_only_script_id, String. Format (scp_number_only_script, base. ID ));}
Why is there page. isclientscriptblockregistered (scp_number_only_script_id? Let's imagine, if there are ten controls in the web page, is it necessary to output ten such scripts? Obviously, this is superfluous. Therefore, we need to use isclientscriptblockregistered () to determine whether the script is output on the client. If the script has been registered on the client, it is no longer output.
The next step is to override the onprerender () method, which is used to draw scripts to the client.
Protected override void onprerender (eventargs e) {base. onprerender (E); renderjavascript ();}
You should note that this script is executed only when an event is triggered. When a user inputs data from a browser, this action is ignored if it is not a number. Otherwise, the input is accepted. This requires the onkeypress = "javascript: Return Policy (this);" code. How can this code be output to the client? Override the addattributestorender () method, which is used to draw component attributes. So we wrote the following code:
Protected override void addattributestorender (htmltextwriter writer) {base. addattributestorender (writer); writer. addattribute ("onkeypress", scp_number_only_hook);} The final source code is as follows: //////////////////////////////////////// //////////////////////////////////////// note, the copyright holder of this Code is Mr. Huang zhongcheng. /// Here, I would like to thank him for writing the book ASP. NET Component Design // //////////////////////////////////////// using system; using system. text; using system. drawing; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace powerasp. net. controls {[toolboxbitmap (typeof (numbereditor), "powerasp.net.controls.numbereditor.bmp")] public class numbereditor: baseeditor {private const string scp_n Umber_only_script_id = "{29fd7a41-49fd-4fc4-afa9-6a0b875a1a51}"; private const string scp_number_only_hook = "Return numbereditor_keypress_handle (this );"; private const string scp_number_only_script = "<script language =/" javascript1.2/">/nfunction numbereditor_keypress_handle (CTRL)/n {/N" + "If (event. keycode = 13)/n return true;/n if (event. keycode <48 | event. keycode> 57)/n return false;/n else/n Return true;/n }}" + "</SCRIPT>"; // rending number-limit Javascript. Private void renderjavascript () {If (! Page. isclientscriptblockregistered (scp_number_only_script_id) page. registerclientscriptblock (scp_number_only_script_id, String. format (scp_number_only_script, base. ID);} protected override void addattributestorender (htmltextwriter writer) {base. addattributestorender (writer); writer. addattribute ("onkeypress", scp_number_only_hook);} protected override void onprerender (eventargs e) {base. onprerender (E); renderjavascript ();} public numbereditor (): Base (){}}}
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