Learning Resources
Â
Automatically Completing Data As the User Types
AutoComplete extends any ASP.NET TextBox control. It associates that control with a popup panel to display words that begin with the prefix that is entered into the text box. When the user has entered more characters than a specified minimum length, a popup displays words or phrases that start with that value. By default, the list of words is positioned at the bottom left side of the text box.
The candidate words that match the user input are supplied by a Web service. Caching is enabled, so if the same characters are entered multiple times, only one call is made to the Web service.
We'll create an auto-complete TextBox by following these steps (1) Add a ToolkitScriptManager (2) Add a TextBox control (3) Add an AutoCompleteExtender (4) Add a page method.
Add a ToolkitScriptManager
Before you can use any of the Ajax Control Toolkit controls in a page, you first need to add a ToolkitScriptManager to the page. You can drag the ToolkitScriptManager from the Visual Studio Toolbox window onto the page. The ToolkitScriptManager is located in the Ajax Control Toolkit tab under the Toolbox.
- <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> Â
- asp:ToolkitScriptManager>Â Â
Add a TextBox Control
The AutoCompleteExtender works with a standard ASP.NET TextBox control. In Design view, drag a TextBox control from under the Standard tab in the Toolbox onto your page.
Next, change the ID of the TextBox control to txtMovie. You can change the ID in the Properties Window. The resulting source code looks like this:
- <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> Â
- asp:ToolkitScriptManager>Â Â
- Â Â
- <asp:TextBox ID="txtMovie" runat="server">asp:TextBox> Â
Add an AutoCompleteExtender
The next step is to apply an AutoCompleteExtender control to the TextBox. Add the following AutoCompleteExtender control to your page:
Â
- <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> Â
- asp:ToolkitScriptManager>Â Â
- Â Â
- <asp:TextBox ID="txtMovie" runat="server">asp:TextBox> Â
- Â Â
- <asp:AutoCompleteExtender  Â
- Â Â Â Â ID="AutoCompleteExtender1"Â Â Â
- Â Â Â Â TargetControlID="txtMovie"Â Â Â
- Â Â Â Â runat="server"Â />Â Â
Add a Page Method
The final step is to create a method that returns the auto-complete suggestions. You can return auto-complete suggestions from an ASMX Web service, a WCF Web service, or a static page method. We use a static page method.
The easiest way to add the static page method is to click on the Add AutoComplete page method smart tag option. Selecting this menu option will create a new page method named GetCompletionList:
VB
-
  - Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String() Â
-   Return Nothing Â
- End Function Â
C#
- [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()] Â
- public static string[] GetCompletionList(string prefixText, int count, string contextKey) { Â
-     return default(string[]); Â
- }Â Â
Notice that this method is passed parameters which represent what the user has typed into the TextBox (prefixText) and the number of auto-complete suggestions to show (count).
The following GetCompletionList() method returns a matching movie from a list of movies:
VB
-
  - Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String() Â
-     ' Create array of movies Â
-     Dim movies() As String = {"Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II"} Â
- Â Â
-     ' Return matching movies Â
-     Return ( Â
-         From m In movies Â
-         Where m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase) Â
-         Select m).Take(count).ToArray() Â
- End Function Â
C#
- [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()] Â
- public static string[] GetCompletionList(string prefixText, int count, string contextKey) { Â
-     // Create array of movies Â
-     string[] movies = {"Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II"}; Â
- Â Â
-     // Return matching movies Â
-     return (from m in movies where m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray(); Â
- }Â
Â