New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Binding to Web service
RadAutoCompleteBox can be bound to Web service. The path to the Web service and the name of the service method are specified in the WebServiceSettings' Path and Method properties:
The text currently present in the input area can be obtained from the context object used by the WebService's method.
JavaScript
<script type="text/javascript">
function requesting(sender, eventArgs) {
var context = eventArgs.get_context();
//Data passed to the service.
context["ClientData"] = "ClientData_Passed_To_The_Service";
}
</script>
ASPNET
<telerik:radautocompletebox runat="server" id="RadAutoCompleteBox1" onclientrequesting="requesting">
<WebServiceSettings Path="LoadEntries.asmx" Method="GetCompanyNames" />
</telerik:radautocompletebox>
To use the integrated support, the Web service should have the following signature:
C#
[WebMethod]
public AutoCompleteBoxData GetCompanyNames(RadAutoCompleteContext context)
{
//Accesses the additional data sent from the client.
string clientData = context["ClientData"].ToString();
string sql = "SELECT * from Customers WHERE CompanyName LIKE '" + context.Text + "%'";
SqlDataAdapter adapter = new SqlDataAdapter(sql,
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
DataTable data = new DataTable();
adapter.Fill(data);
List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
AutoCompleteBoxData dropDownData = new AutoCompleteBoxData();
result = new List<AutoCompleteBoxItemData>();
for (int i = 0; i < data.Rows.Count; i++)
{
AutoCompleteBoxItemData itemData = new AutoCompleteBoxItemData();
itemData.Text = data.Rows[i]["CompanyName"].ToString();
itemData.Value = data.Rows[i]["CustomerID"].ToString();
result.Add(itemData);
}
dropDownData.Items = result.ToArray();
return dropDownData;
}