Search code examples
c#asp.netautocompleteajaxcontroltoolkit

How to select a value from CSV in an asp.net ajax toolbox autocomplete and pass that value to other textbox?


I have used Ajax toolkit autocomplete working fine using the following asp.net and C# code.

 <asp:UpdatePanel ID="updPnlMedicineName" runat="server" >
  <ContentTemplate>
           Name<br />
               <asp:TextBox ID="txtMedName" runat="server" Width="150px" 
                   ontextchanged="txtMedName_TextChanged"></TextBox>
  <cc1:AutoCompleteExtender ID="txtSearchID_AutoCompleteExtender" runat="server"    DelimiterCharacters="" OnClientItemSelected="ItemSelected" Enabled="True" GetMedicine" ServicePath="Search/NameSearch.asmx" TargetControlID="txtMedName" MinimumPrefixLength="1">
 </cc1:AutoCompleteExtender>

My code Behind is :

[WebMethod]
public string[] GetMedicine(string prefixText)
{
 List<string> listString = new List<string>();
 using (SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["NewLandConnectionString"].ConnectionString))
{
 SqlCommand com = new SqlCommand("select ProductName+','+ProductCode+',' + (select unitName from Unit where Unit.Id=product1.UnitId) as ProductDetails,ProductId from product1 where ProductName like '"+prefixText+"%'", con);
 con.Open();
 SqlDataAdapter adr = new SqlDataAdapter(com);
 DataTable dt = new DataTable();
 adr.Fill(dt);
  for (int j = 0; j < dt.Rows.Count; j++)
        {
            string countProduct = dt.Rows[j]["ProductDetails"].ToString();

           listString.Add(AutoCompleteExtender.CreateAutoCompleteItem(dt.Rows[j]["ProductDetails"].ToString(),dt.Rows[j]["ProductId"].ToString()));
        }
  }
    string[] strarray = listString.ToArray();
    return strarray;
 }

Now when my auto complete works i get values in the form of : somename,code,unit but i want that if a value is selected then i must get only the first value from csv i.e "somename" in that texbox and other two values(i.e code,unit ) must be populated in other texboxes respectively.

Please, help me in achieving this task.

Thanks in advance !!


Solution

  • Finally, got the solution myself and with the help of this link , hope this would help someone also.So, I'm providing the code below for my above query:

    OnClientItemSelected on the AutoCompleteExtender control run a javascript function
    
     <script type="text/javascript">
        function ItemSelected(sender, args) {
            __doPostBack(sender.get_element().name, "");
        }
    </script>
    

    Now on text change of the TextBox(AutoExtender) :

     Put the below code on textchange event
    
        string obj = txtMedName.Text.ToString();//in this texbox you are getting your 
                      //values from autoextender and so store this value in to a string
        string[] nameList = obj.Split(','); //finally using split separate your CSV 
                                        //values and store in an array which you are 
                                        // getting on autoextender
        txtMedName.Text = nameList[0].ToString();//Now pass the values to respective 
                                                 //texboxes
        txtBox2.Text = nameList[1].ToString();
        txtBox3.Text = nameList[2].ToString();