Rendercontents for ASP. NET Custom Controls

Source: Internet
Author: User

To bind part of the data in the project to the dropdownlist, you can customize a dropdownlist, even if the custom control inherits from the dropdownlist

A method is defined in the custom control to bind business data, as follows:

 
Public void buildtree (string loginname) {var orgnization = Organization. loadbyuserloginname (loginname); datasource = orgnization. childous; datatextformatstring = getpre (Orgnization) + "{0}"; datatextfield = "name"; datavaluefield = "ID"; databind (); items. insert (0, orgnization. tolistitem ());}

However, after dragging the custom control to the page and calling the buildtree method, it is found that the dropwdownlist content is always empty, and the source data of datasoure is suspected to be empty, however, during tracking and debugging, we found that orgnization. childous has data. At this time, we had to use the reflector decompilation software to check where the items was called.

Because dropdownlist is inherited from listcontrols, the definition of items is in listcontrols, and it is found that items is called in the rendercontents method and will generate the final htmlCodeOption.

At this time, it is found that when a custom control is created, it will automatically generate a method that overwrites rendercontents with the module, as shown below:

 
Protected override void rendercontents (htmltextwriter output) {output. Write (text );}

By default, the template will override the rendercontents method, which is defined in msdn as follows:

Render the control content to the specified writer. htmltextwriter indicates the output stream of HTML content to be displayed on the client.

From this we can see that this method is used to ultimately define the content of the input HTML for the reason, so when developing custom controls, be sure to note that rendercontents has implemented your final HTML output.

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.