我有這個文本框和AutocompleteExtender如何將自定義值傳遞給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:文件.getElementById( 'drpCustomerType')。值「?
誰能告訴我如何將drpCustomerType
的值傳遞給此方法,以便我可以根據該方法顯示項目列表?
沒有它不工作。 'autoComplete1_OnClientPopulating'或者說'sender.set_contextKey(document.getElementById('drpCustomerType')。value);'在firebug中永遠不會被命中。我甚至在此之後添加了警報,但它從未解僱。 – Razort4x 2013-03-12 11:48:28
我的不好。我是在'OnClientPopulated'而不是'OnClientPopulating'中做的。 :-P 它的工作。 – Razort4x 2013-03-12 11:52:35