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

Raz*_*t4x 1 ajax autocomplete ajaxcontroltoolkit

我有这个文本框和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>
Run Code Online (Sandbox Code Playgroud)

并且该方法被定义为

[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();
}
Run Code Online (Sandbox Code Playgroud)

我想要的是传递一个自定义值,文档说我们可以使用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>
Run Code Online (Sandbox Code Playgroud)

并重载方法接受如下,

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

但是当调试器点击它时,我没有得到drpCustomerType相反的值,我得到了硬编码字符串"javascript:document.getElementById('drpCustomerType').value"?

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

Yur*_*kiy 5

您需要OnClientPopulating在JavaScript中处理事件并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" />
Run Code Online (Sandbox Code Playgroud)