Basic concepts of ASP. NET control development

Source: Internet
Author: User

We will continue to discuss some things about ASP. NET control development.

This time we will discuss composite controls. this article cannot be fully described in one step, because the discussion is too long, so I will write it twice. This time I will talk about some basic concepts, and I will write it next time. it seems that there have been many tutorials on the composite controls. I believe you have read a lot. if you have read a friend, you just need to talk nonsense.

1. Concept of composite controls

Composite controls have many similarities with user controls. The biggest difference is that user controls are suffixed with ascx, while composite controls are dll files after compilation and can be distributed to users, for other differences, please refer to MSDN. To put it bluntly, composite controls provide greater flexibility.

2. rendering of composite controls

(1) display of general controls

From the first article to the sixth article, we have introduced the Render and RenderContents methods for displaying controls. Let's take a look at the following code.

 
 
  1. publicoverridevoidRender(HtmlTextWriterwriter)  
  2. {  
  3. ..  
  4. writer.RenderBeginTag(HtmlTextWriterTag.Td);  
  5. writer.AddAttribute(HtmlTextWriterAttribute.Name,"CreditCardNo");  
  6. writer.AddAttribute(HtmlTextWriterAttribute.Id,"CreditCardNo");  
  7. writer.AddAttribute(HtmlTextWriterAttribute.Type,"text");  
  8. writer.RenderBeginTag(HtmlTextWriterTag.Input);  
  9. writer.RenderEndTag();  
  10. writer.RenderEndTag(); 

In the past, all the controls we made were written in this way. We can think that the Render method implements the following two functions.

◆ Tag layout (such as the td tag to make the control display better -_-)
◆ Displayed tags (such as input tags)

(2) method of displaying composite controls

We know ASP. NET control development has provided us with controls like TextBox, just like assembling a computer, you can assemble. net provides your existing controls, and then pieces them together into a new control, which becomes a composite control. however, the rendering method is different.

The composite control is displayed in the form of a child control. The specific presentation is handed over to the child control, because the sub-Control itself has implemented its rendering method (each Control inherits from the Control class ). suppose you still don't understand it. Let's look at the figure below.

LabelTextBox1 is a composite control, while LabelTextBox2 is not. The two are displayed in different ways. For the specific implementation method, see the following

3. Complete basic Composite Control presentation

(1) first, you must familiarize yourself with the following attributes and methods:
◆ The Control. CreateChildControls method is used to create a child Control.
◆ The Control. EnsureChildControls method is used to confirm whether a child Control has been created. If not, call the CreateChildControls method to create a child Control.
◆ The Control. ChildControlsCreated attribute gets a value, whether the child Control has been created

(2) understand and implement the INamingContainer Interface

The UserControl class of the user control inherits the INamingContainer interface. If the package guarantee sub-control has a unique ID name, the composite control also needs to achieve the same purpose, which is worth noting.

The composite control uses the class Writing Method to add the control, namely the CreateChildControls method, instead of implementing the following method in the Render method:

 
 
  1. writer.RenderBeginTag(HtmlTextWriterTag.Input); 

4. control status and performance Selection

In the above example, you will find that after clicking the button, the textbox value status is saved. Do you still remember to directly present the input tag control using the Render method? After clicking the button, the textbox value status cannot be saved.

We have discussed some knowledge about data return, and we have also defined a textbox Control. After clicking the button, we can perfectly save its value status. for the reason, please refer to the following. The author has clearly analyzed the cause.

ASP. NET control development quick tutorial: generate a Composite Control

Although it is relatively easy to create a composite control, the performance system overhead may occur because the child control must be created at the time of writing. To optimize the control performance, you can rewrite the Render method to implement the rendering logic on your own. In addition, you must implement any sending and processing operations required by the control.

I wanted to write the event processing and style together, but I want to write too much space. I will write it here this time, it takes some time to think about how to express the meaning in your heart. next time, we will continue to discuss the events and styles of the composite controls.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Related Article

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.