Initial Design of custom control

Source: Internet
Author: User
[Introduction]
ASP. NET Program What members can do. Developing a handy and well-encapsulated widget is no longer something that can be done by a large team. In this case, some commercial companies are still very powerful and scary, but the price is also scary. As an individual or a small team, you can publish some practical, small, and simple controls. The following describes only the author's understanding of custom control design and some application experience.

What is custom control?
I often compare custom control with JSP taglib. In fact, in my opinion, there are many similarities between the two-similar design ideas. I understand custom control in this way:

1. Released in the form of DLL, which can be used directly.
2. appear as a special server tag in HTML
3. You can set the status through programming.
4. Post Data Processing capability
5. backend events can be triggered.
5. It can be divided into visualization and non-visualization. Specifically, the visual output target HTML can only complete some practical functions.
6. You can set its features through HTML attribute or programming.

(This article is intended to introduce visual controls, especially practical Widgets)

The above 4th entries are particularly important, because since they are used as server-side tags, they must be used during data re-sending.CodeCaptures the status changes or activities of the object.

The purpose of designing custom control is to simplify the coding workload. Note that the convenience provided here is not only for current projects, but may bring traversal to all programmers in the world. Unlike ascx, custom control is used across projects and languages.

I have previously designed a small and relatively simple datepicker custom control. You can pop up a small beautiful calendar when entering date data. After selecting a date, you can automatically write the date to the control on the webpage in the format. This avoids manual input format validation and other troubles. The HTML output by this control is very concise. Only one HTML mark is output. In fact, I handed all my work to the client browser for processing. The scripts used in the calendar pop-up process are encapsulated into an HTC file, and some necessary images and data are all stored on the client. The client here is prone to misunderstanding. It is a client for VB code. In fact, script resources and other static resources are stored on the remote server, but they are called on the client.
Some may ask why.

First. You do not want the HTML generated by the control to pollute your page. Some developers are particularly sensitive to code cleanliness. It is especially annoying for large sections and messy scripts generated by poorly designed controls. If we develop HTML, we will never write such a mess, but separate the script from HTML. In addition, reducing the output HTML can also improve the performance to a certain extent.
Second. Separate the script logic from the VB logic to isolate them for easy maintenance of their respective functions. Sometimes (most chances) We only need to change the supported scripts or HTC, instead of re-compiling VB.
Third. When multiple identical controls appear on the page, the advantage is more obvious. It is not difficult for you to think about what a bunch of identical scripts will look like on the page.

For example. You need a set of buttons in your project. (Few projects do not require buttons ). However, your taste determines that you will never look at the gray button by default in HTML. It looks ugly and lacks creativity. You can easily use CSS to format all your buttons. But you are still not satisfied. The button you want will change as the mouse approaches to please your users. For example, when the mouse is near, it becomes brighter. Press the mouse to go down and darken it. You can also set DHTML behavior for the button. At this time, your boss proposed it. This button is good. But the Red series should be changed in this module, and the blue series should be changed in another module. You are panic. At this time, you need to design the button custom control. Unless you do not think it is difficult to set a series of classes for the control. After the development, you can sell it out. I believe there will be a lot of Boss interested.

It is not difficult to change the color of a button with the mouse. I can provide you with HTC source code for free as long as you need it. You just need to set the style = "behavior: URL (... HTC)" for the button. Now let's think about what the controls should look like. As listed above:

1. compile it into a DLL
2. A custom tag appears on the ASP. NET page.
3. You can use HTML attribute to set how to respond to the mouse action or how the mouse approaches to the desired color.
4. The required scripts and other resources must be found in the HTML output.
5. events must be triggered like backend events. That is, the click event must be triggered like a normal button. It cannot be nice.

Here, you may already know what to do. You must create a class derived from system. Web. UI. webcontrols. webcontrol. The render method must be rewritten. Render is the most critical method. Is the process of HTML output by the control. Before writing this method. You 'd better have a basic idea in your mind. What is the HTML generated by the control and how to work in the foreground. Generally, there is HTC support, and the HTML you need to send to the client should not be much.

The public features are very important. This is related to how many people like your control, or how much it will sell. You should at least let your customer programmers control the way buttons change color. Or will the mouse change color, drum up, make a sound, or run to another place when approaching? You can save these attributes to private members. Output to the client in the render method. For example, a feature of the button class is colorscheme. This value determines a series of color groups, which are the colors of buttons in Mouseover, mousedown, mouseup, and mouseout. Your vbprogram only needs to output this feature to the client as it is. Then, let's talk to your partner who developed HTC or the Script: Let's do it. You need to handle other features. In short, this balance depends on the strong will of the Members. In short, we need to make logic more flexible.

In addition, you must tell the client how the poor HTML looks for its own resources. Generally, I add a feature to the button. Resourcepath, a string that indicates the relative path of the resource to the current page. Then, package the written scripts, images, and other static resources to the user. Tell them to undo these things and copy them to a directory on the Web server, and then set the path to the control.

The HTML output to the client is basically like this:
<Timeline: button colorscheme = "XP"/>

(The above is the html m tag. You need to introduce HTC to the page in advance .)

In addition, you must find a way for the control to trigger events to the backend. I usually attach an invisible form field to the control. This method allows the data sent to the backend to be processed by itself. It will take a long time to discuss this issue. This articleArticleHow much involved a little: http://www.cnblogs.com/jayxu/archive/2004/03/26/4252.aspx attention, in understanding this problem, first do not try to develop. This is because it cannot be used. This issue is explained in detail in the msdn document. Search for the index "cause server events.

Other ideas. When your control is developed. (Skilled friends may not take a day ). You only need to write a tag similar to <asp: button/> to use your control.

Further expansion. You can add the autopostback feature for your button. If it is set to false, the page will not be sent back even if you click the button. In this case, you can add the onclick attribute to the button (different from the event click ), script process for processing this action when setting the client click.

Button is a bit simple. But it can be used as an example study for beginners. The author has many similar works. As long as you have time, you will be happy to write articles for analysis. The author is biased towards application, and the basic skills are not strong. Extensive criticism is welcome.

Email: xujian (a) nwpu.edu.cn
MSN: xchunyu (a) msn.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.