前台代码:
<script type="text/ecmascript">
function OnTxtKeyDown() {
var acNameClientId = "<%=autoComplete1.BehaviorID %>";
var acName = $find(acNameClientId);
if (acName != null) {
acName.set_contextKey('需要传递的参数');
}
}
</script>
<asp:TextBox ID="txt" runat="server" Width="150" onkeydown="return OnTxtKeyDown();"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" BehaviorID="AutoCompleteEx" ID="autoComplete1"
TargetControlID="txt" ServicePath="AutoComplete.asmx" ServiceMethod="Search"
MinimumPrefixLength="0" CompletionInterval="1000" EnableCaching="false" CompletionSetCount="10"
CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" DelimiterCharacters=";, :"
ShowOnlyCurrentWordInCompletionListItem="true">
webservice方法:
[WebMethod]
public string[] GetCompletionList(string prefixText, int count,string contextKey)
{
if (count == 0)
{
count = 10;
}
if (prefixText.Equals("xyz"))
{
return new string[0];
}
Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char) random.Next(65, 90);
char c2 = (char) random.Next(97, 122);
char c3 = (char) random.Next(97, 122);
items.Add(prefixText + c1 + c2 + c3);
}
return items.ToArray();
}
需要注意的是第三个参数名字必须为string contextKey,否则不会进行提示;