AjaxToolKit中的AutoComplete

时间:2013-02-28 14:11:17

标签: asp.net ajax

我想使用AjaxToolKit AutoComplete功能。 标签的语法是:

<ajaxToolkit:AutoCompleteExtender ID="autoComplete1" runat="server"
  EnableCaching="true"
  BehaviorID="AutoCompleteEx"
  MinimumPrefixLength="2"
  TargetControlID="myTextBox"
  ServicePath="AutoComplete.asmx"
  ServiceMethod="GetCompletionList" 
  CompletionInterval="1000"  
  CompletionSetCount="20"
  CompletionListCssClass="autocomplete_completionListElement"
  CompletionListItemCssClass="autocomplete_listItem"
  CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
  DelimiterCharacters=";, :"
  ShowOnlyCurrentWordInCompletionListItem="true">
  <!-- Some formatting code -->
</ajaxToolkit:AutoCompleteExtender>

有ServicePath和ServiceMethod属性可帮助标记从中获取数据。 ServiceMethod具有架构:

[WebMethod]
public string[] GetCompletionList(string prefixText, int count)

该方法只需要两个参数。 对于某些业务逻辑要求,我想将三个参数发送到方法:

[WebMethod]
public string[] GetCompletionList(string type, string prefixText, int count)

如何传递第三个参数并在服务方法中接受它进行处理。 我的结果将取决于此类型参数。 我怎样才能做到这一点? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您可以将contextKey作为第三个参数传递。

设置ajaxToolkit:AutoCompleteExtender时,添加键值对UseContextKey =&#34; True&#34;,例如

<ajaxToolkit:AutoCompleteExtender ID="autoComplete1" runat="server"
  UseContextKey="True"
  EnableCaching="true"
  BehaviorID="AutoCompleteEx"
  MinimumPrefixLength="2"
  TargetControlID="myTextBox"
  ServicePath="AutoComplete.asmx"
  ServiceMethod="GetCompletionList" 
  CompletionInterval="1000"  
  CompletionSetCount="20"
  CompletionListCssClass="autocomplete_completionListElement"
  CompletionListItemCssClass="autocomplete_listItem"
  CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
  DelimiterCharacters=";, :"
  ShowOnlyCurrentWordInCompletionListItem="true">
  <!-- Some formatting code -->
</ajaxToolkit:AutoCompleteExtender>

在调用服务方法之前,将上下文设置为您想要的任何字符串:

function setContextKey() {
    text = 'my type information';
    $find('<%=autoComplete1.ClientID%>').set_contextKey(text);
}

然后在您的代码中,您可以访问该contextKey:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
    string myType = contextKey;
}

答案 1 :(得分:0)

您无法添加第三个参数。但是,您可以通过存储然后从SessionRequest检索此参数信息来阅读此参数信息,方法是从HttpContext.Current访问它们,因为您使用的是静态方法。