使用数据表时,我的ajax auto complete extender代码无效

时间:2016-03-29 08:26:21

标签: c# asp.net asp.net-ajax

这是.aspx代码

  <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"></asp:ScriptManager>
      <asp:TextBox ID="SearchTextBox" runat="server"></asp:TextBox>  
                <ajaxToolkit:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"  
                    CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="SearchTextBox"  
                    ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">  
                </ajaxToolkit:AutoCompleteExtender> 
                 </div>

这是我的cs页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class UserMasterPage : System.Web.UI.MasterPage
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [System.Web.Script.Services.ScriptMethod]
    [System.Web.Services.WebMethod]
    public static List<string> GetCompletionList(string prefixText, int count)
    {
        ConnectionClass cl = new ConnectionClass();
        DataTable dt = cl.AutoComplete(prefixText);
        List<string> Topics = new List<string>();
        using (dt)
        {
            int r = dt.Rows.Count;
            while (r > 0)
            {
                Topics.Add(dt.Rows[r].ItemArray[0].ToString());
                r--;
            }
        }
        return Topics;
}
}

这是我的连接类 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for Class1
/// </summary>
public class ConnectionClass
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str;
    public ConnectionClass()
    {
        str = @"Data source=INSPIRATION\SQLEXPRESS; Initial Catalog=ComputerPedia; Integrated security= true";
    }
    public void establishConnection()
    {
        con = new SqlConnection(str);
        if (con.State == ConnectionState.Closed)
        con.Open();
    }
    public void closeConnection()
    {
        if (con.State == ConnectionState.Open)
        con.Close();
    }
    public void createReaderCommand(string sql)
    {
        cmd = new SqlCommand(sql, con);
        dr = cmd.ExecuteReader();
    }
    public SqlDataReader executeReaderCommand()
    {
        dr.Read();
        return dr;
    }
    public DataTable AutoComplete(string topic)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Topic");
        establishConnection();
        cmd = new SqlCommand("AutoComplete", con);
        cmd.Parameters.Add("@topic", SqlDbType.NVarChar).Value = topic;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        closeConnection();
        return dt;
    }
}

我的存储过程

ALTER procedure [dbo].[AutoComplete]
(
@topic nvarchar(20)
)
as
begin
select distinct Topic from Tutorials where Topic like @topic + '%'
end

我的自动完成扩展程序无效。该过程在sql server中正常工作!请帮我。我不知道我做了什么错。提前谢谢!

1 个答案:

答案 0 :(得分:0)

Try to change your GetCompletionlist as follows and also set CompletionInterval="0"

 [System.Web.Script.Services.ScriptMethod]
    [System.Web.Services.WebMethod]
    public static List<string> GetCompletionList(string prefixText, int count)
    {
        List<string> Topics = new List<string>();
        ConnectionClass cl = new ConnectionClass();
        DataTable dt = cl.AutoComplete(prefixText);
             int i;
       for (i = 0; i < dt.Rows.Count; i++)
           {
               Topics.Add(dt.Rows[i]["ColumnName"].ToString());//Try to give the column name of the resultset from the datatable
           }


        return Topics;
}
相关问题