在autocoompleteextender中显示来自同一行的不同列的两个不同值

时间:2017-01-09 06:57:34

标签: c# asp.net autocompleteextender

我在我的asp.net网站上使用autocompleteextender。它工作正常。我希望在用户输入时显示其他功能,如下所示

假设我在文本框中输入P. 结果 - PNQ,PAR,PBC等

这里evry关键字在第二列中有完整形式,所以我希望数据显示如下

PNQ(浦那机场)

当用户选择此值时,应仅选择文本框中的PNQ而不是整个值,即PNQ(浦那机场)。我希望因为用户应该清楚他们正在选择什么。任何人都可以帮助我。

 <asp:AutoCompleteExtender ServiceMethod="SearchClientCode"
 ServicePath="~/myadmin/shipments-profile.aspx" MinimumPrefixLength="1"
 OnClientShown="resetPosition" CompletionInterval="100" EnableCaching="false"
 CompletionSetCount="10" TargetControlID="clientCode" ID="clientCodeExtender"
 runat="server" FirstRowSelected="false" CompletionListCssClass="completionList"
 CompletionListItemCssClass="listItem"
 CompletionListHighlightedItemCssClass="itemHighlighted"></asp:AutoCompleteExtender>


[System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()]
public static List<string> SearchClientCode(string prefixText, int count)
{
    MySqlConnection conn = new MySqlConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString;
    MySqlCommand cmd = new MySqlCommand();
    cmd.CommandText = "SELECT clientID, clientName FROM clientsDetails where (clientID like @SearchText)";
    cmd.Parameters.AddWithValue("@SearchText", prefixText + "%");
    cmd.Connection = conn;
    conn.Open();
    List<string> customers = new List<string>();
    MySqlDataReader sdr = cmd.ExecuteReader;
    while (sdr.Read) {
        customers.Add(sdr("clientID").ToString);
    }
    conn.Close();
    return customers;
}

1 个答案:

答案 0 :(得分:1)

您需要添加一些客户端功能并更改webmethod以完成您的要求

<强> HTML

将两个事件 OnClientPopulated OnClientItemSelected 连接起来,并设置 BehaviorID 进行控制。

<asp:AutoCompleteExtender ServiceMethod="SearchClientCode" ServicePath="~/myadmin/shipments-profile.aspx" MinimumPrefixLength="1" OnClientShown="resetPosition" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="clientCode" ID="clientCodeExtender" runat="server" FirstRowSelected="false" CompletionListCssClass="completionList" CompletionListItemCssClass="listItem" CompletionListHighlightedItemCssClass="itemHighlighted" OnClientPopulated="onClientPopulated" OnClientItemSelected="itemSelected" BehaviorID="AutoCompleteEx"></asp:AutoCompleteExtender>

<强>的Javascript

<script type="text/javascript">
  function itemSelected(ev)
  {
    var index=$find("AutoCompleteEx")._selectIndex;
    if(index!=-1) {
   $find("AutoCompleteEx").get_element().value =$find("AutoCompleteEx").get_completionList().childNodes[index]._value;
  }
 else{
  $find("AutoCompleteEx").get_element().value = '';   
 }
}

function onClientPopulated(sender,e)
{
  var List=$find("AutoCompleteEx").get_completionList();
  for(i=0;i<List.childNodes.length;i++)
   {
     var _value=JSON.parse(List.childNodes[i]._value);
     var abbr=_value[0];
     var fullform =_value[1];
     List.childNodes[i]._value=abbr;
     List.childNodes[i].innerHTML="<span>"+ abbr + "("+fullform+")</span>"
   }  }</script>

<强>的webmethod:

[System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()]
public static List<string> SearchClientCode(string prefixText, int count)   
 {
   MySqlConnection conn = new MySqlConnection();
   conn.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString;
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT clientID, clientName FROM clientsDetails where (clientID like @SearchText)";
cmd.Parameters.AddWithValue("@SearchText", prefixText + "%");
cmd.Connection = conn;
conn.Open();
List<string> customers = new List<string>();
MySqlDataReader sdr = cmd.ExecuteReader;
JavaScriptSerializer serializer = new JavaScriptSerializer();
while (sdr.Read) {
    object[] item = new object[] { sdr("clientID").ToString(), sdr("clientName").ToString() };
    customers.Add(serializer.Serialize(item));
}
conn.Close();
return customers;}

希望它有所帮助!!