Hi RichardSa,
Refer the example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Default
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="SearchCustomers"
MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtContactsSearch" FirstRowSelected="false" OnClientItemSelected="OnClientItemSelected">
</cc1:AutoCompleteExtender>
<script type="text/javascript">
function OnClientItemSelected(sender, args) {
var selectedName = args._text;
window.location.href = "Home.aspx?Name=" + encodeURI(selectedName);
}
</script>
Home
<asp:Label ID="lblName" runat="server" />
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
Code
Home
C#
[WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> customers = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(sdr["ContactName"].ToString());
}
}
conn.Close();
return customers;
}
}
}
VB.Net
<WebMethod()>
Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefixText)
cmd.Connection = conn
conn.Open()
Dim customers As List(Of String) = New List(Of String)()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(sdr("ContactName").ToString())
End While
End Using
conn.Close()
Return customers
End Using
End Using
End Function
Default
C#
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = HttpUtility.UrlDecode(Request.QueryString["Name"]);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
lblName.Text = HttpUtility.UrlDecode(Request.QueryString("Name"))
End Sub
Screenshot