母版页中的自动完成扩展程序

时间:2010-12-08 22:08:43

标签: asp.net asp.net-ajax

我经历了很多论坛,帖子等。但是从来没有找到真正好的答案。 我正在尝试将AutoComplete Extender添加到TextBox并向用户显示一些提示。 将此代码放入内容页面时,一切都很好。但我有一个基于一个母版页的~10个内容页面,因此在每个母版页上重复代码是完全愚蠢的。

网上有一些答案,但它们只是部分答案,请检查一下你是不是:

Could post only one link :/

这是我的代码:

母版页:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/AutoComplete.asmx" />
    </Services>
</asp:ScriptManager>

<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
            <ajax:AutoCompleteExtender 
                ID="AutoCompleteExtender1" 
                runat="server" 
                TargetControlID="TextBox1"
                ServiceMethod="GetCompletionList"
                ServicePath="~/AutoComplete.asmx"
                MinimumPrefixLength="1"
                CompletionInterval="500"
                CompletionSetCount="2">
            </ajax:AutoCompleteExtender>

WebService的:

/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{        
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        // Create array of movies  
        string[] movies = { "Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II" };

        // Return matching movies  
        return (from m in movies where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray();
    }
}

为了记录,我已经检查了Web服务,它工作得很好

P.S。这是我的第一篇文章很抱歉,如果我做错了什么

1 个答案:

答案 0 :(得分:0)

面对类似的问题,但似乎唯一的解决方案是在内容页面本身编写Webmethod,并且autocompleteextender及其各自的控件可以保存在母版页中。

相关问题