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:
- ﹤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﹥
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:
- 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﹥";
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 AddAttributesToRender). This method 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 of ASP. NET Component Design is as follows:
- 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()
-
- {
-
- }
-
- }
-
- }
I will introduce you to ASP. NET Component Design, and hope to help you understand ASP. NET Component Design.
- Summary of ASP. NET control development
- Development of ASP. NET template controls
- Development of ASP. NET data binding controls
- Analysis of ASP. NET control design support
- Analysis of ASP. NET control designer