ASP. NET itself provides a lot of controls for our lazy people to use. I think the role of the control is here, because we don't want to repeat the work, so we need to create it, this is a demand relationship, so I learned ASP. NET control development is very interesting.
It hasn't been published yet, but the code is released on the website, so I just downloaded it and learned it. I have read the code in the previous chapters, and the author uses different knowledge to show us the same effect. So it is very helpful to learn it step by step. although you have configured ASP. NET control development is not very familiar, but I feel that the following points are very important and I have summarized them myself.
1. Understand the inheritance relationship between controls. It is best to look at the System. Web. UI namespace first.
(1) Control class: A class shared by all controls. You need to check several protected methods and attributes in the class. Although you can't finish it, in the future, we will find that these methods are often used. You can look at their derived classes on MSDN.
(2) One class that the HtmlTextWriter class has to understand is mainly used to mark characters and text output.
2. Rewrite Method
(1) The Control class must be inherited.
(2) rewrite the Render method of the Control class. This is required. Because other controls inherit the Control class, almost all controls have this method.
3. Be familiar with metadata
Everyone knows ASP. NET control attributes are classified in the editor, such as appearance, behavior, layout, etc. Each attribute also provides an explanation of simple metadata to play this role, of course, you can also not add, however, when metadata is used, it makes people feel friendly.
To use metadata under [CategoryAttribute ("Appearance")], you must reference the System. ComponentModel naming control. Generally, if you write components, you cannot use such class libraries. Here is an introduction to MSDN.
1. Output string
It's boring to say a little more. Let's continue the drill. First, you need to understand HTML. The following code outputs HTML to the client.
The effect is very simple. In fact, we have been adding several attributes to the output HTML. You can directly put the code in the App_Code folder to automatically compile the code. Of course, you can also create a web control library.
Note that you must inherit the Control class, override the Render method, and use the Write of the HtmlTextWriter class to output HTML
Use Controls
(1). Register it first.
- <%@ Register TagPrefix="custom" Namespace="CustomComponents" %>
(2) Use tags to output results.
- <custom:CreditCardForm1 runat="server" ID="ccf" />
Ii. Improvement: adding attributes and metadata
The controls made above may be useless, but they can familiarize you with the steps. The controls above are very dead and have no defined attributes. They are of little use. We will modify them below.
Let's define common attributes and then output them so that we can modify the attributes of the control.
3. Improve again, eliminate the use of the Write method to output HTML in string Mode
Then we continue to find the problem. We found that apart from defining several attributes that need to be modified by ourselves, we still need to use a large number of strings to output HTML, and it is easy to lose errors. the HtmlTextWriter class provides several useful methods for replacement.
(1) Add style attributes to tags using AddStyleAttribute
(2) Add attributes to tags using the AddAttribute Method
(3) RenderBeginTag starts to write the label header, as shown in <table...>
(4) write the RenderEndTag to the end of the tag, as shown in </table>
Pay special attention to the following points.
1. because its definition method is different from our usual definition method, when we write HTML, we first write the beginning of the tag and then the attributes of the tag. for example, <table borderwidth = "0">. However, when we use the preceding methods, we need to define the attributes and styles of the labels first, and then output the label header.
2. The label header and tail must correspond one to one. It can be understood as a nested relationship. The best way to understand this is to view the source file after the code is output, and then look at the original defined code.
It is easier to see the code. Because CreditCardForm2 has defined the required attributes, all we need to do now is to replace the string format in the form of tags, so we only need to inherit the CreditCardForm2 class, rewrite the Render method.
Iv. Consequences of unused view status
View status. For details about view status, refer to MSDN and related articles.
Let's take a look at the following example: CreditCardForm3
- if(!IsPostBack)
- {
- creditcardform.CardholderNameText="FullName";
- creditcardform.CreditCardNoText="CreditCardNo";
- creditcardform.ExpirationDateText="ExpirationDate";
- creditcardform.PaymentMethodText="PaymentOptions";
- creditcardform.SubmitButtonText="Send";
The above introduces ASP. NET control development
- Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
- ASP. NET Server custom control security guidelines
- Analysis of ASP. NET programming standards and Their encoding specifications
- Introduction to ASP. NET sessions
- ASP. NET programming tool ASP. NET Web Matrix