Original: http://blog.csdn.net/moremoreTea1983/archive/2007/06/13/1651273.aspx
The ajaxcontroltoolkit series controls released with Asp.net Ajax 1.0 bring a lot of convenience to developers, but many of them seem not as perfect as we think. Recently, many AutoComplete controls have been used here and several shortcomings or errors have been found:
1. In some cases, the error "two components with the same ID" may occur;
2. It is easy to see "unable to open the Internet site ,..., Operation terminated;
3. Even if the user has entered many characters, even if no matching result exists, it will still call the server method to try to obtain the matching value, which increases the burden on the server in vain;
4. the style of the automatically completed list is not very nice;
5. The signature of the server method must be string [] getcompletionlist (string prefixtext, int count). Other required data cannot be obtained from the client. This is especially fatal when multiple AutoComplete controls on a page need to obtain data from different data sources.
To solve these problems, you must first know how to modify the correspondingCodeAnd make it take effect in your own applications. Fortunately, the ajaxcontroltoolkit controls are open-source, so we can modify them as needed. Use vs2005 to open the ajaxcontroltoolkit solution and open autocompletebehavior In the autocomplete folder. JS file, modify and re-compile the generated ajaxcontroltoolkit. update the DLL file to your project reference, and you can apply the autocomplete control after optimization and enhancement.
So what code should I modify?
To solve the first problem, add the following before ajaxcontroltoolkit. autocompletebehavior. callbasemethod (this, 'dispose:
If (this. _ popupbehavior ){
This. _ popupbehavior. Dispose ();
This. _ popupbehavior = NULL;
}
The second problem is to change document. Body. appendchild (this. _ completionlistelement) to element. parentnode. appendchild (this. _ completionlistelement );
The third problem is that in the _ ontimertick method, if (text. trim (). length <this. _ minimumprefixlength) adds a condition to the judgment to be: If (text. trim (). length <this. _ minimumprefixlength | text. trim (). length> 10), so that when the user input exceeds 10 characters, you do not have to call the server to read the matching value.
To adjust the automatic Completion list style, you can directly modify the following code in the initializecompletionlist method:
Completionliststyle. backgroundcolor = This. _ textbackground;
Completionliststyle. Color = This. _ textcolor;
Completionliststyle. Border = 'solid 1px buttonshadow ';
Completionliststyle. cursor = 'default ';
Completionliststyle. unselectable = 'unselectable ';
Completionliststyle. Overflow = 'ddd ';
Or delete the rows and add: element. classname = "completionlist". Then, add the Style Class "completionlist" on the page;
in {prefixtext: This. _ currentprefix, Count: This. _ completionsetcount} added a parameter sent to the server in the row, which changed to:
{prefixtext: This. _ currentprefix, Count: This. _ completionsetcount, srcid: This. get_element (). getattribute ("srcid")}
As a result, the method signature of the list item automatically completed by the server can be written as: String [] getcompletionlist (string prefixtext, int count, string srcid ). This means that we can pre-set an identifier string for the text box that requires the Automatic completion function to identify its data source, C # Code such as TB. attributes. add ("srcid", "XXX"); then, in the getcompletionlist method, you can read data according to the parameter value passed by the client.