1

我有这个文本框和 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();
}

我想要的是传递一个自定义值,文档说我们可以使用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给这个方法,以便我可以根据它显示项目列表?

4

2 回答 2

6

您需要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" />
于 2013-03-12T10:57:11.687 回答
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 
于 2014-10-18T11:07:00.660 回答