vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Saturday, March 23, 2013

Ajax control Toolkit AutoCompleteExtender working with webservice



Here I would like to share ajax control autocomplete extender.By using with this control you can easily attached to any textbox to display suggestion list to the user.
You can get the suggestion list from the database or from XML file to display..
Here is the code to link textbox with AutoCompleteExtender
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"
                ServiceMethod="GetCompletionList" EnableCaching="true" ServicePath="~/Retrunlist.asmx"
                UseContextKey="True" MinimumPrefixLength="1" CompletionInterval="100" CompletionSetCount="10">
            </asp:AutoCompleteExtender>
        </ContentTemplate>
    </asp:UpdatePanel>


Properties
·  TargetControlID - The TextBox control where the user types content to be automatically completed
·  ServiceMethod - The web service method to be called. The signature of this method must match the following:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count) { ... }
Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.
·  ServicePath - The path to the web service that the extender will pull the word\sentence completions from. If this is not provided, the service method should be a page method.
·  ContextKey - User/page specific context provided to an optional overload of the web method described by ServiceMethod/ServicePath. If the context key is used, it should have the same signature with an additional parameter named contextKey of type string:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(
    string prefixText, int count, string contextKey) { ... }
Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.
·  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). If the context key is used, it should have the same signature with an additional parameter named contextKey of type string (as described above).
·  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. The text in the AutoComplete textbox is tokenized using these characters and the webservice completes the last token.
·  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.
Webservice code:
[WebMethod]

    public string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        List<string> collections = new List<string>();

        DataSet ds = new DataSet();

        ds.ReadXml(HttpContext.Current.Server.MapPath("sample.xml"));

        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];


                if (dr[0].ToString().StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase))
                {
                    collections.Add(dr[0].ToString());

                    if (collections.Count == count) break;
                }

            }

        }
          return collections.ToArray();
            }


XML File structure:
Sample.xml
<?xml version="1.0" encoding="utf-8" ?>
<country>
  <name>India</name>
  <name>Iran</name>
  <name>America</name>
  <name>Afhanistan</name>
  <name>Russia</name>
  <name>Pakistan</name>
  <name>United Kingdom</name>
  <name>Dubai</name> 
</country>

Now you can run above code autocomplete extender give the suggestion list by entering text on the textbox.
Here you can Download the complete code for the Project

0 comments:

Post a Comment