Question

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.

enter image description here

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>
Was it helpful?

Solution

You can use javascript in order to achieve that, add the "onChange" attribute to the text box and when the text box value changes then directly change the label text.

Put this in Page_Load event in code behind:

this.tbDrugName.Attributes.Add("OnChange", "tbDrugName_OnChange()");

And add this javascript function in markup:

        function tbDrugName_OnChange() {
            var txtVal = document.getElementById('<%= tbDrugName.ClientID %>').value;
            document.getElementById('<%= lblList.ClientID %>').innerText = txtVal;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top