光标点

时间:2017-01-23 07:01:52

标签: javascript c# asp.net autocompleteextender

我使用autocompleteextender进行文本框自动完成功能。我已经为建议添加了js&价值选择。问题是在键入一些字母后,如果我从上到下移动光标,那么它不会选择光标指向的值。它选择光标位置上方的值。仅当光标从上到下移动时才会出现此问题。从底部移动到顶部时工作正常。

在下面的快照中,您可以看到我的光标位置和位置。它突出了上面的价值。 enter image description here

    <asp:TextBox ID="clientCode" runat="server" CssClass="field-pitch" ClientIDMode="Static"></asp:TextBox>

    <asp:AutoCompleteExtender ServiceMethod="SearchClientCode"
 ServicePath="~/auto.aspx" MinimumPrefixLength="1" 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>

            <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 + Convert.ToString("%"));
    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;
}

1 个答案:

答案 0 :(得分:0)

您应该使用OnClientItemSelected方法的给定参数:

function itemSelected(source, eventArgs) {
    $find("AutoCompleteEx").get_element().value = eventArgs.get_value();
}