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

时间:2013-03-12 09:53:51

标签: 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>

该方法定义为

[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的值传递给此方法,以便我可以根据该方法显示项目列表?

2 个答案:

答案 0 :(得分:5)

您需要在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" />

答案 1 :(得分:0)

 You can do it using Hidden Field, In Code Behind you didnot pass ContextKey anywhere
 Find the session and store it in hidden field at page load that will be your drpCustomerType 



   Do like this in WebMethod 

   [System.Web.Services.WebMethod (EnableSession=true)]

      contextkey= Convert.ToInt32(HttpContext.Current.Session["drpCustomerType "].ToString());
        cmd.Parameters.AddWithValue("@drp ", contextkey);

     @drp = Variable Declare in SP