You need to use DataList OnItemDataBound Event to attach the TargetControlId and DropDownList OnSelectedIndex change event to Set the Context key.
HTML
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:DataList ID="dlItems" runat="server" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged"
AutoPostBack="true">
<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>
<asp:TextBox ID="txtContactsSearch" runat="server"></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>
</ItemTemplate>
</asp:DataList>
<asp:Button Text="Save" runat="server" />
</div>
Namespaces
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using AjaxControlToolkit;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Rows.Add();
this.dlItems.DataSource = dt;
this.dlItems.DataBind();
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
DataListItem item = (sender as DropDownList).NamingContainer as DataListItem;
AutoCompleteExtender autoCompleteExtender = item.FindControl("AutoCompleteExtender1") as AutoCompleteExtender;
autoCompleteExtender.ContextKey = (sender as DropDownList).SelectedItem.Value;
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = e.Item.FindControl("txtContactsSearch") as TextBox;
DropDownList ddl = e.Item.FindControl("ddlCountries") as DropDownList;
AutoCompleteExtender autoCompleteExtender = e.Item.FindControl("AutoCompleteExtender1") as AutoCompleteExtender;
autoCompleteExtender.ContextKey = ddl.SelectedItem.Value;
autoCompleteExtender.TargetControlID = txt.ID;
autoCompleteExtender.ServiceMethod = "SearchCustomers";
autoCompleteExtender.UseContextKey = true;
}
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.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 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(sdr["ContactName"].ToString());
}
}
conn.Close();
return customers;
}
}
}
Screenshot