TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

Friday, February 23, 2007 #

Just a heads up if you use the AutoCompleteExtender Ajax control. I'm not sure if this is because I'm using Visual Studio SP1 (using Web Application Projects), but I could not get this control to work with an ASMX, but rather a WebMethod in the page's code-behind. Also, make sure your ServiceMethod attribute declaration is static!

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

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
<ajaxToolkit:AutoCompleteExtender runat="server"
                                  ID="autoComplete1"
                                  TargetControlID="myTextBox"
                                  ServiceMethod="GetCompletionList"
                                  MinimumPrefixLength="1"
                                  CompletionInterval="1000"
                                  EnableCaching="true"
                                  CompletionSetCount="12" />
</form>

 

public partial class _Default : System.Web.UI.Page {
    [WebMethod]
    public static string[] GetCompletionList(string prefixText, int count) {
        string sql = String.Format("select companyName from customers where companyname like @companyname + '%'");

        List<string> companyList = new List<string>();
        using (SqlConnection connection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=."))
        using (SqlCommand command = new SqlCommand(sql, connection)) {
            connection.Open();
            command.Parameters.AddWithValue("@companyname", prefixText);
            using (SqlDataReader reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    companyList.Add(reader.GetString(0));
                }
            }
        }

        return companyList.ToArray();
    }
}

 

 

posted @ 2:20 PM | Feedback (3)