简体   繁体   中英

Use autocomplete to fetch data from Database in ASP.NET Web Forms with c#?

I am modifying an existing application developed by another programmer using ASP.NET Web Forms and C#.

I have to add to a simple TextBox the autocomplete functionality. When the user starts to input something into the TextBox , the TextBox should show suggestions based on the data stored in a database. The suggestions have to be based just on the prefix (the initial part of the words already inputed).

I found many examples based on the autocomplete AJAX extender but it is based on fetching data froma Web Service. What are the steps and required methods and operations in order to fulfill this task?

Link you posted is pretty much explains everything that needs to be done . Follow this video .

If you need to pass some additional parametes check this example.

Steps.

1.Add Textbox and Ajax Extender to page and specify target id as textboxid .Specify web service name to extender
2.Implement webservice as in link above and test it in browser .
3.In webservice write the way you want to retrieve data . You will be returning a string[] .

Instead of creating webservice you can even use webmethod attribute to a regular method check following

http://www.ajaxtutorials.com/ajax-tutorials/using-autocomplete-in-the-ajax-toolkit/
http://allwrong.wordpress.com/2007/03/13/ms-ajax-autocomplete-extender-using-a-page-method/

You can simply define a method on the page codebehind, decorate it with the [WebMethod] attribute and then set it on the ServiceMethod property of the dropdown extender.
The method must implement the logic to retrieve/filter the results and its signature must match the examples (eg: public string[] MyMethod(string prefixText, int count)).

The Prerequisite is to have "AjaxControlToolKit".

First we need to place the textbox in the updatepanel so that partial postback can happen thereby eliminating the entire page reload. Use the ajax autocompleteExtender using which we can invoke a service method which is having DB call which will fetch data and populate the textbox.

       <asp:UpdatePanel ID="pnlAcct" runat="server">
  <ContentTemplate>
  <asp:TextBox ID="txtAcctNum"></asp:TextBox>
  <asp:AutoCompleteExtender ID="AutoCompleteExtenderAccount" runat="server" MinimumPrefixLength="1" ServiceMethod="GetSourceAccount" ServicePath="~/AutoComplete/AutoComplete.asmx"
                                    TargetControlID="txtAcctNum" Enabled="True" CompletionSetCount="20" CompletionInterval="1000"
                                    EnableCaching="true">
                                </asp:AutoCompleteExtender>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="txtAcctNum" />
                            </Triggers>
                        </asp:UpdatePanel>

The service call can be done by adding a WebService(.asmx file) and writing the below code in .asmx.cs file.What ever data available in the string will be displayed below the textbox.

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
     public string[] GetSourceAccount(string prefixText, int count)
     {
  List<string> lstSimilarSource = new List<string>();
  //Service call and populating the string
  lstSimilarSource = Autocomplete.GetSimilarSource(prefixText, "ACCOUNT");
   return lstSimilarSource.ToArray();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM