在asp.net中使用ajax autocompleteextender控件

时间:2011-05-17 07:38:58

标签: asp.net

我正在使用ajax AutoCompleteExtender控件并使用.net 3.5。我在工具箱选项卡中成功添加了dll 首先,我创建一个网站,然后在我的default.aspx页面中拖动脚本管理器,文本框和AutoCompleteExtender控件。然后我添加webservice并定义一个Web方法,即GetSuggestions()。我的程序执行成功,但我没有得到正确的输出。当我在文本框中放置任何文本时,它不会填充任何建议。请帮我。

这是我的网络服务代码:

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)]

public class MyAutocompleteService : WebService
{
    [WebMethod]

    public string[] GetSuggestions(string prefixText, int count)
    {
        List<string> responses = new List<string>();

        for (int i = 0; i < count; i++)

            responses.Add(prefixText + (char)(i + 65));

        return responses.ToArray();

    }

}                                                                                               

这是我的default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services >
        <asp:ServiceReference Path="~/MyAutocompleteService.asmx" />
        </Services>
        </asp:ScriptManager>
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />

        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
            ServiceMethod="GetSuggestions" ServicePath="MyAutocompleteService.asmx" 
            TargetControlID="TextBox1" MinimumPrefixLength ="1" 
            CompletionSetCount ="12" EnableCaching="true">
        </asp:AutoCompleteExtender>
        <br />
        <br />

    </div>
    </form>
</body>
</html>

所以请在我失踪的地方帮助我。

2 个答案:

答案 0 :(得分:2)

要查看来自Web服务的结果,您必须添加将填充结果的div。我编辑了上面的一些代码。

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    **<div id="resultsPanel"></div>**
    <br />

    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
        ServiceMethod="GetSuggestions" ServicePath="MyAutocompleteService.asmx" 
        TargetControlID="TextBox1" MinimumPrefixLength ="1" 
        CompletionSetCount ="12" EnableCaching="true"
        **CompletionListElementID="listPlacement"**>
    </asp:AutoCompleteExtender>

我希望有所帮助。

答案 1 :(得分:0)

尝试使用没有“〜”符号的路径

<asp:ServiceReference Path="MyAutocompleteService.asmx" />