【问题标题】:How to pass a custom value to AutocompleteExtender?如何将自定义值传递给 AutocompleteExtender?
【发布时间】:2014-10-19 12:37:26
【问题描述】:

我有这个文本框和 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 的值传递给这个方法,以便我可以根据它显示项目列表?

【问题讨论】:

    标签: ajax autocomplete ajaxcontroltoolkit


    【解决方案1】:

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

    【讨论】:

    • 不,它不起作用。 autoComplete1_OnClientPopulating 或更确切地说是 sender.set_contextKey(document.getElementById('drpCustomerType').value); 在萤火虫中永远不会被击中。在那之后我什至添加了一个警报,但它从未触发过。
    • 我的错。我是在OnClientPopulated 而不是OnClientPopulating 做的。 :-P 它正在工作。
    【解决方案2】:
     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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多