I have the AutoCompleteExtender that autocompletes tbDrugName. So far, it pulls DrugIds from my database. What I want is for it to autocomplete DrugNames and on selection, display DrugId in a lbllist label next to it.
This is my cs code:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetDrugNames(string prefixText, int count, string contextKey)
{
SqlConnection sc1 = new SqlConnection();
sc1.ConnectionString = "Data Source=localhost;Initial Catalog=Drug;Integrated Security=True";
sc1.Open();
/*Opens connection to drugTable and does string matching*/
SqlCommand getdrugnames = new SqlCommand("Select * from DrugTable where DrugID like @Name+'%'", sc1);
getdrugnames.Parameters.AddWithValue("@Name", prefixText);
List<string> DrugNames = new List<string>();
using (SqlDataReader sdr = getdrugnames.ExecuteReader())
{
while (sdr.Read())
{
string item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(sdr["DrugID"].ToString(), sdr["DrugName"].ToString());
//item.drugid, item.drugname
DrugNames.Add(item);
//DrugNames has list objects
}
}
return DrugNames;
}
This is my aspx code:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<asp:TextBox ID="tbDrugName" runat="server" ></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender
ID="AutoCompleteDrugID" runat="server"
TargetControlID="tbDrugName"
ServiceMethod="GetDrugNames"
MinimumPrefixLength="1"
CompletionInterval="200"
UseContextKey="True"
>
</ajaxToolkit:AutoCompleteExtender>
</ajaxToolkit:AutoCompleteExtender>
<asp:Label ID="lbllist" runat="server" Text="Label"></asp:Label>
</td>