2013-02-28 110 views
0

我想使用AjaxToolKitAutoComplete功能。 爲標籤的語法是:AjaxToolKit中的自動完成

<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) 

如何傳遞這個第三個參數,並在業務處理方法接受它。 我的結果將取決於此類型參數。 我該如何做到這一點? 在此先感謝。

回答

0

您不能添加第三個參數。但是,您可以通過從SessionRequest中存儲然後從HttpContext.Current訪問它們來讀取此參數信息,因爲您處於靜態方法。

+0

你確定嗎?在我的情況下,我有四個單選按鈕,我想在返回列表時考慮檢查哪一個。我不想在整個過程中做任何回傳。我們如何確定檢查哪個單選按鈕。 – MaxRecursion 2013-02-28 14:18:03

+0

在'GetCompletionList'方法中添加一個斷點並觀察'HttpContext.Current.Request',您將看到所有的表單輸入。這樣工作,因爲當該方法被調用時,瀏覽器向服務器執行請求併發送所有表單數據。將會有單選按鈕的值 – 2013-02-28 14:24:46

1

您可以傳遞contextKey作爲第三個參數。

當設置ajaxToolkit:AutoCompleteExtender,添加鍵值對UseContextKey =「真」,如

<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; 
}