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