Implementation of ASP. NET Component Design Code

Source: Internet
Author: User

What is the implementation of ASP. NET Component Design Code? 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﹥ 

ASP. the concept of NET component design is to think about users, because these verification codes cannot be written by users and should be written by component designers. That is to say, when you drag 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 return false;\n else\n return true;\n}}"+  
  10.  
  11. "﹤/script﹥"; 

ASP. NET Component Design implementation verification code generation:

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

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 OnPreRender () method is rewritten for ASP. NET Component Design and implementation. This method 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;\n 
  32. 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,string.Format(SCP_NUMBER_ONLY_SCRIPT,base.ID));  
  44.  
  45. }  
  46.  
  47. protected override void AddAttributesToRender(HtmlTextWriter writer)  
  48.  
  49. {  
  50.  
  51. base.AddAttributesToRender(writer);  
  52.  
  53. writer.AddAttribute("OnKeyPress",SCP_NUMBER_ONLY_HOOK);  
  54.  
  55. }  
  56.  
  57. protected override void OnPreRender(EventArgs e)  
  58.  
  59. {  
  60.  
  61. base.OnPreRender (e);  
  62.  
  63. RenderJavaScript();  
  64.  
  65. }  
  66.  
  67. public NumberEditor():base()  
  68.  
  69. {  
  70.  
  71. }  
  72.  
  73. }  
  74.  

The implementation of ASP. NET component design is introduced here. I hope to help you understand ASP. NET Component Design.

  1. Analysis of ASP. NET configuration error page
  2. Analysis on making ASP. NET error pages
  3. Analysis of folder permission settings in ASP. NET Website settings
  4. Advantages of ASP. NET
  5. Analysis on ASP. NET Component Design

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.