AutocompleteExtender不能在母版页中工作

问题描述:

我有一个关于ASP.NET中的自动完成扩展器的问题 - 它在我拥有的所有页面中工作正常,但不在母版页中,我不知道为什么。AutocompleteExtender不能在母版页中工作

这里是我的代码:

<asp:TextBox runat="server" ID="txtSearch" Width="200px" CssClass="TextBoxClass"></asp:TextBox> 
<cc1:AutoCompleteExtender ID="txtSearch_AutoCompleteExtender" runat="server" 
    TargetControlID="txtSearch" 
    CompletionInterval="0" 
    CompletionListCssClass="autocomplete_completionListElement" 
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" 
    CompletionListItemCssClass="autocomplete_listItem" 
    CompletionSetCount="10" EnableCaching="true" MinimumPrefixLength="2" 
    ServiceMethod="GetCompletionListOggetti" 
    ShowOnlyCurrentWordInCompletionListItem="true" UseContextKey="True"> 
</cc1:AutoCompleteExtender> 

后面的代码:

<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _ 
Public Shared Function GetCompletionListOggetti(ByVal prefixText As String, ByVal count As Integer) As String() 
    ' Insert code to return a string array here… 
    Return AutoCompleteOggetti(prefixText) 
End Function 

的问题是,GetCompletionListOggetti不会被调用。

我再说一遍 - 它在内容页面上工作正常!提前致谢。

我通过把web方法(在你的情况下,GetCompletionListOggetti)放在内容页面代码背后的代码而不是母版页上来管理它。而且它仅适用于文件后面相同代码中的web方法,而不适用于单独的asmx服务。为此,请不要忘记将EnablePageMethods =“true”属性添加到脚本管理器。

看起来AutoCompleteExtender的服务方法在用户(或自定义)控件的代码内部定义的时候并不会被调用,而且母版页确实是一种控件。

此修复程序的缺点是您必须在使用此母版页的每个内容页面中放置相同的服务方法。不是很优雅。另一个缺点是建议dropdownlist的css无法正常工作。尽管如此,我仍然无法弄清楚周围的情况。有人更好的主意?

+0

“此修复程序的缺点是,你必须把相同的服务方法,在使用该母版页EVERY内容页面。”遗产? – ProfK 2013-07-01 17:42:33

您需要设置AutoCompleteExtender的ServicePath属性来覆盖回调到加载(非主页)页面的默认行为。

添加您的函数到web服务页面(的.asmx),虚拟页面,或Default.aspx的等的代码隐藏

如果使用web服务页面,你将需要添加/取消注释行:

<System.Web.Script.Services.ScriptService()> _ 

为VB,或C#

[System.Web.Script.Services.ScriptService]