Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Design of ASP.NET component

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to implement ASP.NET component design". In daily operation, I believe many people have doubts about how to implement ASP.NET component design. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to implement ASP.NET component design". Next, please follow the editor to study!

What is the design code implementation of ASP.NET components? Suppose we are designing a component that only allows the user to enter numbers, and the validation work should naturally be placed on the client side, where the validation 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 >

The idea of ASP.NET component design is to think of the user, because the validation code cannot be written by the user, but should be written by the component designer, that is, when the user drags the component from the toolbox to the page, the runtime should automatically generate the validation code. By drawing the code to the WEB page, we can rewrite the OnPreRender () method.

Before overriding the OnPreRender () method, write and define a few constants:

Private const string SCP_NUMBER_ONLY_SCRIPT_ID= "{29FD7A41-49FD-4fc4-AFA9-6A0B87***1A51}"; 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;\ nif (event.keyCode < 48 | event.keyCode > 57)\ n return false;\ n else\ n return true \ n} "+" < / script > "

Generation of verification code for ASP.NET component design implementation:

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 that if there were ten of these controls in the WEB page, would we have to output ten such scripts? Obviously, this is superfluous, so we use IsClientScriptBlockRegistered () to determine whether the script is output on the client side, and if the script is registered on the client side, it is no longer output.

The ASP.NET component design implementation overrides the OnPreRender () method, which is responsible for drawing scripts to the client.

Protected override void OnPreRender (EventArgs e) {base.OnPreRender (e); RenderJavaScript ();}

You should note that the script needs to be triggered by an event to execute, and when the user enters data from the browser, if it is non-numeric, the action is ignored, otherwise the input is accepted. This requires the code OnKeyPress= "_ javascript:return Virty (this);". So, how does this code output to the client? Override the AddAttributesToRender () method, which is responsible for drawing the properties of the component. So we wrote the following code:

Protected override void AddAttributesToRender (HtmlTextWriter writer) {base.AddAttributesToRender (writer); writer.AddAttribute ("OnKeyPress", SCP_NUMBER_ONLY_HOOK);}

The source code for the design and implementation of the ASP.NET component of * 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-6A0B87***1A51}"; 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;\ nif (event.keyCode < 48 | | event.keyCode > 57)\ n return false;\ nelse\ 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 () {} at this point, the study on "how to implement ASP.NET component design" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 209

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report