Autocomplete Extender is offer for suggestion list when user type into textbox a popup display the word that begin with prefix type into textbox
How to use Autocomplete extender
1. Add ToolScriptManager
First understand the properties of Autocomplete extender
2. Add Autocomplete extender to you page
3.Now add this line to at top of your page
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp"%>
4.Add webservice to your project then write the below code to it
How to use Autocomplete extender
1. Add ToolScriptManager
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>First understand the properties of Autocomplete extender
AutoComplete Properties
TargetControlID :- The TextBox control where the user types content to be
automatically completed
ServiceMethod :- The path to the web
service that the extender will pull the word\sentence completions from.
ServicePath:- The path to the web service that
the extender will pull the word\sentence completions from.
ContextKey:- User/page specific context
provided to an optional overload of the web method described by
ServiceMethod/ServicePath.
UseContextKey:- Whether or not the ContextKey
property should be used. This will be automatically enabled if the ContextKey
property is ever set (on either the client or the server).
MinimumPrefixLength:- Minimum number of characters that
must be entered before getting suggestions from the web service.
CompletionInterval:- Time in milliseconds when the
timer will kick in to get suggestions using the web service.
EnableCaching:- Whether client side caching is
enabled.
CompletionSetCount:- Number of suggestions to be
retrieved from the web service.
CompletionListCssClass: Css Class that will be used to
style the completion list flyout.
CompletionListItemCssClass:- Css Class that will be used to
style an item in the AutoComplete list flyout.
CompletionListHighlightedItemCssClass:- Css Class that will be used to
style a highlighted item in the AutoComplete list flyout.
DelimiterCharacters:- Specifies one or more
character(s) used to separate words.
FirstRowSelected:- Determines if the first option in
the AutoComplete list will be selected by default.
ShowOnlyCurrentWordInCompletionListItem:- If true and DelimiterCharacters
are specified, then the AutoComplete list items display suggestions for the
current word to be completed and do not display the rest of the tokens.
Animations:-Generic animations for the
AutoComplete extender.
2. Add Autocomplete extender to you page
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
CompletionInterval="50"
MinimumPrefixLength="1"
ServiceMethod="GetCompletionList"
ServicePath="wsCountry.asmx"
TargetControlID="txtcountry" CompletionListItemCssClass="autoc">
</asp:AutoCompleteExtender>
3.Now add this line to at top of your page
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp"%>
4.Add webservice to your project then write the below code to it
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be
called from script, using ASP.NET AJAX, uncomment the following line.
//
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.ScriptService()]
public class country : System.Web.Services.WebService
{
[WebMethod]
public string[]
GetCompletionList(string prefixText, int count)
{
//"Select EmpName from Emp where EmpName like '"
+ prefixText + "%'"
string[] list =
{ "Afghanistan",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Czech Republic",
"Democratic Republic of
Congo",
"Dominican Republic",
"East Timor",
"Ecuador",
"Egypt",
"El Salvador",
"French Guyana",
"French Polynesia",
"French Southern
Territories",
"Gabon",
};
Array.Sort(list, new
CaseInsensitiveComparer());
int index = Array.BinarySearch(list,
prefixText, new CaseInsensitiveComparer());
if (index < 0)
{
index = ~index;
}
int matchingCount;
for (matchingCount = 0; matchingCount < count
&& index + matchingCount < list.Length; matchingCount++)
{
if (!list[index +
matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
{
break;
}
}
String[] returnCities = new
string[matchingCount];
if (matchingCount > 0)
{
Array.Copy(list, index, returnCities, 0,
matchingCount);
}
return returnCities;
}
}
5. Now run your program and enjoy
No comments:
Post a Comment