【问题标题】:How to select a value from CSV in an asp.net ajax toolbox autocomplete and pass that value to other textbox?如何在 asp.net ajax 工具箱自动完成中从 CSV 中选择一个值并将该值传递给其他文本框?
【发布时间】:2013-03-27 11:18:12
【问题描述】:

我使用以下 asp.net 和 C# 代码使用了 Ajax 工具包自动完成功能。

 <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>

我的代码后面是:

[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;
 }

现在,当我的自动完成工作时,我会以以下形式获取值:somename,code,unit 但我希望如果选择了一个值,那么我必须只从 csv 中获取第一个值,即该 texbox 中的“somename”,而其他两个值(即 code、unit )必须分别填充到其他 texbox 中。

请帮助我完成这项任务。

提前致谢!!

【问题讨论】:

    标签: c# asp.net autocomplete ajaxcontroltoolkit


    【解决方案1】:

    最后,在 this link 的帮助下,我自己得到了解决方案,希望这也能帮助别人。所以,我为我的上述查询提供以下代码:

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

    现在关于 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多