Hi alhakimyyes,
Check the below sample and modify your code accordingly.
Database
For this sample I am making use of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server" Height="30px" Style="font-size: large;
font-weight: 700" Width="235px" autocomplete="off"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtContactsSearch"
UseContextKey="true" ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</cc1:AutoCompleteExtender>
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="All Countries" Value="0"></asp:ListItem>
<asp:ListItem Text="Argentina" Value="Argentina"></asp:ListItem>
<asp:ListItem Text="Austria" Value="Austria"></asp:ListItem>
<asp:ListItem Text="Belgium" Value="Belgium"></asp:ListItem>
<asp:ListItem Text="Brazil" Value="Brazil"></asp:ListItem>
<asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
<asp:ListItem Text="Denmark" Value="Denmark"></asp:ListItem>
<asp:ListItem Text="Finland" Value="Finland"></asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Germany" Value="Germany"></asp:ListItem>
<asp:ListItem Text="Ireland" Value="Ireland"></asp:ListItem>
<asp:ListItem Text="Italy" Value="Italy"></asp:ListItem>
<asp:ListItem Text="Mexico" Value="Mexico"></asp:ListItem>
<asp:ListItem Text="Norway" Value="Norway"></asp:ListItem>
<asp:ListItem Text="Poland" Value="Poland"></asp:ListItem>
<asp:ListItem Text="Portugal" Value="Portugal"></asp:ListItem>
<asp:ListItem Text="Spain" Value="Spain"></asp:ListItem>
<asp:ListItem Text="Sweden" Value="Sweden"></asp:ListItem>
<asp:ListItem Text="Switzerland" Value="Switzerland"></asp:ListItem>
<asp:ListItem Text="UK" Value="UK"></asp:ListItem>
<asp:ListItem Text="USA" Value="USA"></asp:ListItem>
<asp:ListItem Text="Venezuela" Value="Venezuela"></asp:ListItem>
</asp:DropDownList>
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Services;
VB.Net
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Script.Services
Imports System.Web.Services
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AutoCompleteExtender1.ContextKey = ddlCountries.SelectedValue;
}
}
[ScriptMethod()]
[WebMethod]
public static List<string> SearchCustomers(string prefixText, int count, string contextKey)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
string cmdText = "select CustomerID,ContactName from Customers where ContactName like '%'+ @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
if (contextKey != "0")
{
cmdText += " and Country = @Country";
cmd.Parameters.AddWithValue("@Country", contextKey);
}
cmd.CommandText = cmdText;
cmd.Connection = conn;
conn.Open();
List<string> customers = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerID"]));
}
}
conn.Close();
return customers;
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
AutoCompleteExtender1.ContextKey = ddlCountries.SelectedValue;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
AutoCompleteExtender1.ContextKey = ddlCountries.SelectedValue
End If
End Sub
<ScriptMethod(), _
WebMethod()> _
Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As List(Of String)
Using conn As SqlConnection = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand()
Dim cmdText As String = "select CustomerID,ContactName from Customers where ContactName like '%'+ @SearchText + '%'"
cmd.Parameters.AddWithValue("@SearchText", prefixText)
If contextKey <> "0" Then
cmdText += " and Country = @Country"
cmd.Parameters.AddWithValue("@Country", contextKey)
End If
cmd.CommandText = cmdText
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(String.Format("{0}-{1}", sdr("ContactName"), sdr("CustomerID")))
End While
End Using
conn.Close()
Return customers
End Using
End Using
End Function
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
AutoCompleteExtender1.ContextKey = ddlCountries.SelectedValue
End Sub
Screenshot