gpt4 book ai didi

ajax - 如何将自定义值传递给 AutocompleteExtender?

转载 作者:行者123 更新时间:2023-12-01 22:40:20 25 4
gpt4 key购买 nike

我有这个文本框和 AutocompleteExtender

<asp:TextBox ID="txtItemName" runat="server" ClientIDMode="Static"
MaxLength="300" onfocus="javascript:select();"
></asp:TextBox>
<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender"
CompletionListItemCssClass="AutoExtenderList" CompletionListHighlightedItemCssClass="AutoExtenderHighlight"
>
</cc1:AutoCompleteExtender>

方法定义为

[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
List<string> items = new List<string>(count);
SqlCommand con = new SqlCommand();
SqlDataReader sdr = null;
DataSet ds = new DataSet();
try
{
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "proc_NewBooking";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@BranchId", Globals.BranchID);
cmd.Parameters.AddWithValue("@ItemName", prefixText.ToString());
cmd.Parameters.AddWithValue("@Flag", 11);
sdr = AppClass.ExecuteReader(cmd);
ds = AppClass.GetData(cmd);
while (sdr.Read())
{
items.Add("" + sdr.GetValue(0));
}
sdr.Close();
sdr.Dispose();
}
catch (Exception ex)
{
// Log the message and continue
}
return items.ToArray();
}

我想要的是传递一个自定义值,documentation说我们可以使用 contextKey 属性来做到这一点

因此,我将这一行添加到 AutocompleteExtender

<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
ContextKey="javascript:document.getElementById('drpCustomerType').value"
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender"
CompletionListItemCssClass="AutoExtenderList" CompletionListHighlightedItemCssClass="AutoExtenderHighlight"
>
</cc1:AutoCompleteExtender>

并重载了接受它的方法,如下所示,

 public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
// custom code based on context key
}

但是当调试器命中它时,我没有得到 drpCustomerType 的值,而是得到了硬编码字符串“javascript:document.getElementById('drpCustomerType').value”?

谁能告诉我如何将 drpCustomerType 的值传递给此方法,以便我可以显示基于它的项目列表?

最佳答案

您需要在 JavaScript 中处理 OnClientPopulating 事件并在处理程序中设置 contextKey 属性值:

function autoComplete1_OnClientPopulating(sender, args) {
sender.set_contextKey(document.getElementById('drpCustomerType').value);
}


<cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtItemName" EnableCaching="true"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
UseContextKey="true"
CompletionInterval="10" CompletionSetCount="15" FirstRowSelected="True" CompletionListCssClass="AutoExtender"
CompletionListItemCssClass="AutoExtenderList"
OnClientPopulating="autoComplete1_OnClientPopulating"
CompletionListHighlightedItemCssClass="AutoExtenderHighlight" />

关于ajax - 如何将自定义值传递给 AutocompleteExtender?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15357994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com