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:
- ﹤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﹥
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:
- 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﹥";
ASP. NET Component Design implementation verification 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 OnPreRender () method is rewritten for ASP. NET Component Design and implementation. This method 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()
-
- {
-
- }
-
- }
-
- }
The implementation of ASP. NET component design is introduced here. I hope to help you understand ASP. NET Component Design.
- Analysis of ASP. NET configuration error page
- Analysis on making ASP. NET error pages
- Analysis of folder permission settings in ASP. NET Website settings
- Advantages of ASP. NET
- Analysis on ASP. NET Component Design