Component Design On Asp.net

  Component Design On Asp.net 

  One, what is the component? 

  Show MSDN, Microsoft is the definition of components to this:. NET Framework, the component is a means to achieve System.ComponentModel.IComponent interface of a class, or the class to achieve IComponent directly or indirectly derived classes.    This is a pure language (technical) point of the definition, popular speakers, components, is "independent operation of the software modules", stressed here operate independently, it must have represented the low coupling components, high reusability of features .    Microsoft software will be divided into two parts: One is the Component, which refers to a specific function, can operate independently, and do not have UI interface module and the other is Control, that is, we often say that the controls, which refers to a specific function , the independent operation of the UI interface unit. 

  Second, learning Asp.net components need to know 

  An arbitrary master. Net language, the proposed use of C #, C # is a new language, but from the C + + and JAVA grammar, at the same time introducing a number of new concepts in the mouth of beer good programmers. 

  IIS understanding of the operation of the mechanism and operation mode asp.net. 

  Mastering javascript, the scripting language in dealing with the powerful features of the client to take action extremely well, basically all custom components are inseparable from the javascript At the same time, CSS and DHTML is to knows quite well.    No way, they will rarely appeared individually, always like collective performances. 

  Third, the difficulty of Component Design 

  Not asked this question, you might guess a bit, one word: difficult. 

  You may have noticed, in the preparation of asp.net application, few would viewstate for a more detailed study, the reason is very simple, because ViewState itself designed user objects have not Application programmers, but component designers.    If it is not because the client needs, you will not asp.net javascript in the preparation of a large number of scripts, and in the design of components, it is difficult to escape to do.    More than these, whether designed as a server component?    We are the components of succession Control, or inheritance or succession WebControl Component?    In components, it is necessary to customize Attribute?    The need for data binding?    How to draw the appearance of components?    IIS communications and how?    Need post-back?    Many, many problems, we need designers components - hard you consider to January 1. 

  So, if you ignored them and said: Design is not a component?    What this difficult!    Well, I will Hei hei laugh, because I know you will certainly joking. 

  However, tens of millions of Biebo, "programmers need to explore the spirit!" 

  Fourth, the choice of base class 

  If we WEB design is a visual component, and part of a WEB page, then type can be inherited or WebControl Control category.    If it is a non-visual component, can be inherited Component, the succession of such controls will not appear in the design page, but appeared in the Component Tray.    Remember OpenFileDialog control?    This is a file open dialog box controls that appear in the Component Tray Control. 

  If we only have the controls on the basis of enhanced features, then inherit the existing controls it. 

  V. From practice comes real knowledge 

  We must assume that the design of a component, the component only allows users to input figures, the certification work should naturally put client, the client's verification script can be written like this: 

<HTML>

<HEAD>

  <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 

  <TITLE> </ TITLE> 

  <script Language="javascript"> 

  Function Virty (ctrl) 

  ( 

  If (event.keyCode == 13) 

  Return true 

  If (event.keyCode <48 | | event.keyCode> 57) 

  Return false; 

  Else 

  Return true; 

  ) 

  </ Script> 

  </ HEAD> 

<BODY>

  <form Method="POST"> 

<p>

  <input Type="text" name="T1" size="20" OnKeyPress="javascript:return Virty(this);"> 

  </ P> 

  </ Form> 

  </ BODY> 

  </ HTML> 

  These, of course, the code can not be verified by the user to write, it should be by the component designer to write, that is, when the user to drag components from the toolbox on the page, the operation should be automatically generated test code.    WEB page to the drawing code, we rewrite OnPreRender () method on it. 

  In rewriting OnPreRender () method prior to the definition written several constants: 

  Private const string SCP_NUMBER_ONLY_SCRIPT_ID = "(29FD7A41-49FD-4fc4-AFA9-6A0B875A1A51)"; 

  Private const string SCP_NUMBER_ONLY_HOOK = "return Virty (this);"; 

  Private const string SCP_NUMBER_ONLY_SCRIPT = 

  "<script Language=\"JavaScript1.2\"> \ nfunction Virty (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>"; 

  Below the method used to validate the generated code: 

  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)?    We imagine what, if WEB page 10 of the controls, it is not output to 10 this script?    Obviously, this is superfluous, and therefore, we should IsClientScriptBlockRegistered () to determine whether the script output in the client, if the client has been in the script registration is no longer exported. 

  The next step was to rewrite OnPreRender () method, the method is responsible for drawing the script to the client. 

  Protected override void OnPreRender (EventArgs e) 

  ( 

  Base.OnPreRender (e); 

  RenderJavaScript (); 

  ) 

  We should note that the script would need to trigger the implementation of the incident, when users input data from the browser, if not, while the number of neglect of the action, or else they would accept input.    This requires OnKeyPress = "javascript: return Virty (this);" This code has.    Well, this code to the client how the output?    Rewriting AddAttributesToRender () method bar, the method is responsible for mapping component attributes.    So, we wrote the following piece of code: 

  Protected override void AddAttributesToRender (HtmlTextWriter writer) 

  ( 

  Base.AddAttributesToRender (writer); 

  Writer.AddAttribute ( "OnKeyPress" SCP_NUMBER_ONLY_HOOK); 

  ) 

  Finally the source are as follows: 

  ////////////////////////////////////////////////// /////////////////////////// 

  / / / Note that the code for copyright Mr. Huang Zhong. 

  / / / In this he expressed thanks 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_NUMBER_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 () 

  ( 

  ) 

  ) 

  ) 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “Component Design On Asp.net”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.