我在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;
}

最佳答案

您需要添加一些客户端功能并更改网络方法才能满足您的要求

HTML:

连接两个事件OnClientPopulated和OnClientItemSelected,并将BehaviorID设置为control。

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


Java脚本

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


网络方法:

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


希望能帮助到你!!

10-06 02:22