Hello Guys,
Here is an Example of AutoComplete Extender, see how it
works.
Here is my .aspx page
code.
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="AutoComplete.aspx.cs"
Inherits="AutoComplete"
%>
<%@ Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit"
TagPrefix="cc1"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AutoComplete
Extender</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scmAutoCompleteExndr" runat="server">
<Services>
<asp:ServiceReference Path="~/AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:TextBox ID="txtAutoComplete" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender"
runat="server"
TargetControlID="txtAutoComplete"
MinimumPrefixLength="1"
ServiceMethod="GetLocation"
ServicePath="AutoComplete.asmx">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
Here I have created my web service page AutoComplete.asmx
page and below is the .cs code of that
(in App_Code) directory
using System;
using
System.Collections;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
using
System.Web.Services.Protocols;
using
System.Data;
using
System.Data.SqlClient;
///
/// Summary description for AutoComplete
///
[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. //remove the below comments to enable script on
[System.Web.Script.Services.ScriptService]
public class AutoComplete
: System.Web.Services.WebService {
public
AutoComplete () {
//Uncomment
the following line if using designed components
//InitializeComponent();
}
[System.Web.Script.Services.ScriptMethod()]
[WebMethod]
//the method
parameter name should be prefixText
public string[] GetLocation(string
prefixText)
{
clsDAL
objDal = new clsDAL();
string
query = "SELECT AreaName FROM AreaTable WHERE
AreaName LIKE '" + prefixText + "%'";
DataTable
dtLocation = objDal.GetDataTable(query);
if
(dtLocation.Rows.Count > 0)
{
string[]
loc = new string[dtLocation.Rows.Count];
int
indx = 0;
foreach
(DataRow drLocation in
dtLocation.Rows)
{
loc.SetValue(drLocation["AreaName"].ToString(), indx);
indx++;
}
return
loc;
}
else
{
return
null;
}
}
}
Note:
The more interesting part is in auto complete extender is
the methods parameter (string prefixText), even I was not
aware of that why it not works (for me it was shocking moment) when I spent
time more than 2 hours(even I had already done it). Then I got to know we must
pass the parameter as string prefixText then only it works.
And another property is MinimumPrefixLength
to be set the number of character you pass in control.
I hope it will help you..
Comments