Asp.net autocompleteextender 选择问题与光标点
Asp.net autocompleteextender selection issue with cursor point
我已将 autocompleteextender 用于文本框自动完成。我已经为建议和价值 selection 编写了附加 js。问题是在键入一些字母后,如果我将光标从上到下移动,那么光标指向的位置没有 select 值。它 selects 值高于光标位置。只有当光标从上到下移动时才会出现此问题。从下往上移动时工作正常。
在下面的快照中,您可以看到我的光标位置和上面突出显示的值。
<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;
}
您应该使用 OnClientItemSelected 方法的给定参数:
function itemSelected(source, eventArgs) {
$find("AutoCompleteEx").get_element().value = eventArgs.get_value();
}
我已将 autocompleteextender 用于文本框自动完成。我已经为建议和价值 selection 编写了附加 js。问题是在键入一些字母后,如果我将光标从上到下移动,那么光标指向的位置没有 select 值。它 selects 值高于光标位置。只有当光标从上到下移动时才会出现此问题。从下往上移动时工作正常。
在下面的快照中,您可以看到我的光标位置和上面突出显示的值。
<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;
}
您应该使用 OnClientItemSelected 方法的给定参数:
function itemSelected(source, eventArgs) {
$find("AutoCompleteEx").get_element().value = eventArgs.get_value();
}