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

What are the knowledge points of ASP.NET component design?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the knowledge points of ASP.NET component design". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ASP.NET component design 1. What is an ASP.NET component?

Looking at MSDN, Microsoft defines components as follows: in .NET Framework, a component is a class that implements the System.ComponentModel.IComponent interface, or a class that derives directly or indirectly from a class that implements IComponent. This is the definition from the perspective of pure language (technology). Popularly speaking, components are "software units that can operate independently", which emphasizes independent operation, which means that components must have the characteristics of low coupling, high reusability and so on. Microsoft divides the software into two parts: one is Component, which refers to units with specific functions and can operate independently without UI interface, and the other is Control, which refers to UI interface units with specific functions and can operate independently.

Design of ASP.NET components II. Knowledge needed to learn ASP.NET components

Any grasp of a. Net language, it is recommended to use Candlesome C # is a new language, but also draw lessons from the grammar of C++ and JAVA, while introducing some new concepts, beer is good for programmers.

Understand how IIS works and how ASP.NET works.

Proficient in javascript, the powerful scripting language performs very well when dealing with client actions, almost all custom components are inseparable from javascript, at the same time, CSS and DHTML are also well known. There is no way. They seldom show up alone and always like to perform in groups.

ASP.NET component design III. Difficulty of ASP.NET component design

You don't have to ask this question, maybe you have guessed a little bit, in one word: difficult.

You may notice that when writing ASP.NET applications, there is little in-depth research into viewstate, simply because the user objects designed by ViewState itself are not application programmers, but component designers. You wouldn't write a lot of javascript scripts in ASP.NET if it wasn't for client needs, and it's hard to get away with component design. More than that, is it designed as a server component? Does our component inherit Control, WebControl or Component? Do you need to customize Attribute in the component? Do you need to implement data binding? How do I draw the appearance of a component? How to communicate with IIS? Do you need post-back? Many, many problems need component designers-- hard work for you to consider one by one.

So, if you disdain to say: isn't it all about designing a component? It's not so hard! Well, I will smile, because I know that you must be joking.

But don't be afraid, "programmers need to explore the spirit!"

ASP.NET component design IV. Selection of base class

If we design a WEB visual control that forms part of the WEB page, we can inherit the Control class or the WebControl class. If it is a non-visual control, you can inherit Component, and the control that inherits this class does not appear on the page at design time, but in Component Tray. Remember the OpenFileDialog control? This file opens the dialog control that appears in the Component Tray control.

If we just enhance the functionality on the basis of the existing control, then inherit the existing control.

ASP.NET component design 5. Practice leads to true knowledge

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 >

Of course, the validation code cannot be written by the user, but by the component designer, that is, when the user drags the component from the toolbox onto 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 > "

The following methods are used to verify the generation of 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)? 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 next step is to override 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 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-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 () {} "what are the knowledge points of ASP.NET component design"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 0

*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