Creating the Data Lookup Web Service

Creating the Data Lookup Web Service

To implement AutoComplete functionality in an ASP.NET application, you need to create a web service or a page method that returns a list of suggested items based on the user input. Here are the steps to create a web service:

In Visual Studio, create a new ASP.NET Web Application project.

Right-click on the project in Solution Explorer and select Add -> New Item.

Select Web Service from the list of templates and name the file “AutoComplete.asmx”.

Open the “AutoComplete.asmx” file and add a new web method that will be called by the AutoComplete extender control. For example:

[WebMethod]

public string[] GetAutoCompleteData(string prefix)

{

    List<string> results = new List<string>();

    // TODO: Query the database or data source to get a list of suggested items

    // based on the prefix string.

    return results.ToArray();

}

In the web method, query the database or data source to get a list of suggested items based on the prefix string. You can use LINQ to SQL, Entity Framework, or any other data access technology to retrieve the data. For example:

NorthwindDataContext db = new NorthwindDataContext();

var query = from c in db.Customers

            where c.CompanyName.StartsWith(prefix)

            select c.CompanyName;

results.AddRange(query);

Convert the list of suggested items to an array of strings and return it from the web method.

return results.ToArray();

Save the “AutoComplete.asmx” file and build the project.

Once you have created the web service, you can bind it to the AutoComplete extender control by setting its ServicePath and ServiceMethod properties. For example:

<ajaxToolkit:AutoCompleteExtender ID=”autoComplete1″ runat=”server”

    TargetControlID=”txtSearch”

    ServicePath=”~/AutoComplete.asmx”

    ServiceMethod=”GetAutoCompleteData”

    MinimumPrefixLength=”2″

    CompletionInterval=”1000″

    CompletionSetCount=”10″>

</ajaxToolkit:AutoCompleteExtender> In this example, the ServicePath property is set to “~/AutoComplete.asmx”, which specifies the path to the web service file. The ServiceMethod property is set to “GetAutoCompleteData”, which specifies the name of the web method that returns the list of suggested items. The MinimumPrefixLength property is set to “2”, which specifies the minimum number of characters that the user must enter before the AutoComplete feature is activated. The CompletionInterval property is set to “1000”, which specifies the delay in milliseconds before the AutoComplete list is displayed. The CompletionSetCount property is set to “10”, which specifies the maximum number of items that can be displayed in the AutoComplete list.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu