Ajax中的自动完成功能不起作用?

时间:2011-11-14 04:48:51

标签: asp.net

在Ajax中,我在我的asp.net应用程序中使用autocompleteExender,我为此编写服务,当我运行该服务时它工作正常,当我将autocompleteextender放在asp.net页面并为ajax autocompleteextender分配适当性时它不管用。这是我的代码服务:

[WebMethod]
public string[] GetCompletionList(string prefixText)
{
    SqlConnection con=new SqlConnection
     ("server=******;database=Mydb;user id=***;password=****;");
    string sql = "Select productname from F_Product
      Where productname like '" + prefixText + "%'";
    SqlDataAdapter da = new SqlDataAdapter(sql, con); 
     try
    {
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr[0].ToString(), i);
            i++;
        }
        return items; 
    }
    catch
    {
        return null;
    }
    finally
    {
        con.Close();

    }

这是我的ajax autocompleteextender代码。

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" MinimumPrefixLength="2"
     TargetControlID ="TextBox1" ServiceMethod="GetCompletionList"
       ServicePath="~/Autocomplete.asmx"
         runat="server">
    </asp:AutoCompleteExtender> 
    <asp:TextBox ID="TextBox1" runat="server"
     Width="213px"></asp:TextBox>

2 个答案:

答案 0 :(得分:1)

试试这个。

[WebMethod]
    public string[] GetCompletionList(string prefixText)
    {

        string sql = "Select productname from F_Product Where productname like @prefixText ";
        SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["productname"].ToString(), i);
            i++;
        }
        return items;
    }

如果您发现它有用,请将其标记为您的答案,否则请告诉我......

答案 1 :(得分:0)

    using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Script.Services;



namespace YourProject
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    // [ScriptService]
    //[System.Web.Script.Services.ScriptService()]
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [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 WebService : System.Web.Services.WebService
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string[] GetCompletionList(string prefixText)
        {

            string sql = "Select productname from F_Product Where productname like @prefixText ";
            SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);
            da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
            DataTable dt = new DataTable();
            da.Fill(dt);
            string[] items = new string[dt.Rows.Count];
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                items.SetValue(dr["productname"].ToString(), i);
                i++;
            }
            return items;
        }
 }
}

如果您发现它有用,请将其标记为您的答案,否则请告诉我......