Introduction to ASP. NET Component Design

Source: Internet
Author: User

Before learning ASP. NET Component Design, let's first understand what ASP. NET components are.

ASP. NET Component Design 1. What is an ASP. NET component?

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 general, components are "software units that can be operated independently, 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.

ASP. NET Component Design 2. knowledge required to learn ASP. NET components

Master any one. 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 IIS Running Mechanism and ASP. NET running mode.

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.

ASP. NET Component Design III. Difficulty in ASP. NET Component Design

You don't need to ask this question. You may have guessed a few points, one word: hard.

You may be aware that ASP. NET Applications, viewstate is rarely studied in depth, the reason is very simple, because the user object designed by ViewState itself is not an application programmer, but 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 from the components 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 !"

ASP. NET Component Design IV. Selection of base classes

If we design a WEB visual Control that forms 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.

ASP. NET Component Design 5. Practical Knowledge

Suppose we want to design a component that only allows users to enter numbers. The verification work should naturally be put on the client. The client verification script can be written as follows:

 
 
  1. ﹤HTML﹥  
  2.  
  3. ﹤HEAD﹥  
  4.  
  5. ﹤META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"﹥  
  6.  
  7. ﹤TITLE﹥﹤/TITLE﹥  
  8.  
  9. ﹤script language="javascript"﹥  
  10.  
  11. function Virty(ctrl)  
  12.  
  13. {  
  14.  
  15. if (event.keyCode == 13)  
  16.  
  17. return true  
  18.  
  19. if (event.keyCode ﹤ 48 || event.keyCode ﹥ 57)  
  20.  
  21. return false;  
  22.  
  23. else  
  24.  
  25. return true;  
  26.  
  27. }  
  28.  
  29. ﹤/script﹥  
  30.  
  31. ﹤/HEAD﹥  
  32.  
  33. ﹤BODY﹥  
  34.  
  35. ﹤form method="POST" ﹥  
  36.  
  37. ﹤p﹥  
  38.  
  39. ﹤input type="text" name="T1" size="20" OnKeyPress="javascript:return Virty(this);"﹥  
  40.  
  41. ﹤/p﹥  
  42.  
  43. ﹤/form﹥  
  44.  
  45. ﹤/BODY﹥  
  46.  
  47. ﹤/HTML﹥ 

Of course, the Verification Code cannot be written by the user, but should be written by the component designer. That is to say, when the user drags the component from the toolbox to the page, the verification code should be automatically generated during running. To draw code to the web page, we can rewrite the OnPreRender () method.

Before rewriting the OnPreRender () method, write and define several constants:

 
 
  1. private const string SCP_NUMBER_ONLY_SCRIPT_ID="{29FD7A41-49FD-4fc4-AFA9-6A0B875A1A51}";  
  2.  
  3. private const string SCP_NUMBER_ONLY_HOOK="return Virty(this);";  
  4.  
  5. private const string SCP_NUMBER_ONLY_SCRIPT=  
  6.  
  7. "﹤script language=\"JavaScript1.2\"﹥\nfunction Virty (ctrl)\n{{\n"+  
  8.  
  9. "if (event.keyCode == 13)\n return true;\n if (event.keyCode ﹤ 48 || event.keyCode ﹥ 57)\n   
  10. return false;  
  11. \n else\n return true;\n}}"+  
  12.  
  13. "﹤/script﹥"; 

The following method is used to verify code generation:

 
 
  1. private void RenderJavaScript()  
  2.  
  3. {  
  4.  
  5. if(!Page.IsClientScriptBlockRegistered(SCP_NUMBER_ONLY_SCRIPT_ID))   
  6. Page.RegisterClientScriptBlock  
  7. (SCP_NUMBER_ONLY_SCRIPT_ID,  
  8. string.Format(SCP_NUMBER_ONLY_SCRIPT,base.ID));  
  9.  
  10. }  

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.

 
 
  1. protected override void OnPreRender(EventArgs e)  
  2.  
  3. {  
  4.  
  5. base.OnPreRender (e);  
  6.  
  7. RenderJavaScript();  
  8.  

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 AddAttributesToRender). This method is used to draw component attributes. So we wrote the following code:

 
 
  1. protected override void AddAttributesToRender(HtmlTextWriter writer)  
  2.  
  3. {  
  4.  
  5. base.AddAttributesToRender(writer);  
  6.  
  7.  
  8. writer.AddAttribute("OnKeyPress",SCP_NUMBER_ONLY_HOOK);  
  9.  

The final source code of ASP. NET Component Design is as follows:

 
 
  1. using System;  
  2.  
  3. using System.Text;  
  4.  
  5. using System.Drawing;  
  6.  
  7. using System.Web;  
  8.  
  9. using System.Web.UI;  
  10.  
  11. using System.Web.UI.WebControls;  
  12.  
  13. namespace PowerASP.NET.Controls  
  14.  
  15. {  
  16.  
  17. [ToolboxBitmap(typeof(NumberEditor),"PowerASP.NET.Controls.NumberEditor.bmp")]  
  18.  
  19. public class NumberEditor:BaseEditor  
  20.  
  21. {  
  22.  
  23. private const string SCP_NUMBER_ONLY_SCRIPT_ID="{29FD7A41-49FD-4fc4-AFA9-6A0B875A1A51}";  
  24.  
  25. private const string SCP_NUMBER_ONLY_HOOK="return NumberEditor_KeyPress_Handle(this);";  
  26.  
  27. private const string SCP_NUMBER_ONLY_SCRIPT=  
  28.  
  29. "﹤script language=\"JavaScript1.2\"﹥\nfunction NumberEditor_KeyPress_Handle(ctrl)\n{{\n"+  
  30.  
  31. "if (event.keyCode == 13)\n return true;
  32. \n if (event.keyCode ﹤ 48 || event.keyCode ﹥ 57)\n return false;\n else\n return true;\n}}"+  
  33.  
  34. "﹤/script﹥";  
  35.  
  36. //rending number-limit javaScript.  
  37.  
  38. private void RenderJavaScript()  
  39.  
  40. {  
  41.  
  42. if(!Page.IsClientScriptBlockRegistered(SCP_NUMBER_ONLY_SCRIPT_ID)) 
  43. Page.RegisterClientScriptBlock(SCP_NUMBER_ONLY_SCRIPT_ID,
  44. string.Format(SCP_NUMBER_ONLY_SCRIPT,base.ID));  
  45.  
  46. }  
  47.  
  48. protected override void AddAttributesToRender(HtmlTextWriter writer)  
  49.  
  50. {  
  51.  
  52. base.AddAttributesToRender(writer);  
  53.  
  54. writer.AddAttribute("OnKeyPress",SCP_NUMBER_ONLY_HOOK);  
  55.  
  56. }  
  57.  
  58. protected override void OnPreRender(EventArgs e)  
  59.  
  60. {  
  61.  
  62. base.OnPreRender (e);  
  63.  
  64. RenderJavaScript();  
  65.  
  66. }  
  67.  
  68. public NumberEditor():base()  
  69.  
  70. {  
  71.  
  72. }  
  73.  
  74. }  
  75.  

I will introduce you to ASP. NET Component Design, and hope to help you understand ASP. NET Component Design.

  1. Summary of ASP. NET control development
  2. Development of ASP. NET template controls
  3. Development of ASP. NET data binding controls
  4. Analysis of ASP. NET control design support
  5. Analysis of ASP. NET control designer

Related Article

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.