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.