none
autocompleteextender com webservice RRS feed

  • Pergunta

  • to usando o objeto autocompleteextender do ajax, porém não estou conseguindo fazer funcionar como nos exemplos na net.

    abaixo meu html

                        

    <%@ Register A

    ssembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

        <form id="form1" runat="server">


    <asp:ScriptManager ID="ScriptManager1" runat="server" 
    EnablePageMethods = "true">
    </asp:ScriptManager>

        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
            <cc1:AutoCompleteExtender ID="AutoCompleteExtender2"
            BehaviorID="AutoComplete" TargetControlID="TextBox1"
           ServicePath="AutoComplete.asmx"
            ServiceMethod="GetDesignations" MinimumPrefixLength="1"       
             runat="server">                
            </cc1:AutoCompleteExtender>


        </form>

    meu webservice

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication1
    {
        /// <summary>
        /// Summary description for AutoComplete
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        //[WebService(Namespace = "http://localhost/")]
        [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]
            public string[] GetDesignations(string prefixText)
            {
                SqlConnection con = new
                SqlConnection("server=local;integrated security=true;database=master");
                con.Open();
                string strQuery = "select * from tabela where coluna like '" + prefixText + "%'";
                DataSet ds = new DataSet();
                SqlDataAdapter dta = new SqlDataAdapter(strQuery, con);
                dta.Fill(ds);
                con.Close();
                List<string> desigsArList = new List<string>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    desigsArList.Add(ds.Tables[0].Rows[i]["coluna1"].ToString());
                }
                return desigsArList.ToArray();
            }
        }
    }

    só que quando vou digitar alguma coisa na caixa de texto não funciona.

    o que está faltando?


    • Editado rafa-martin quinta-feira, 29 de novembro de 2012 12:51
    quinta-feira, 29 de novembro de 2012 11:24

Respostas

  • mais estou usando ajax. de qualquer forma ue já consegui resolver. na verdade eu não tinha setado a propriedade CompletionInterval no objeto autocomplete.ou seja, estava funcionando. porém, demorava para aparecer a lista na tela. abaixo segue código funcionando.

    html

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="auto_completeajax.aspx.cs" Inherits="WebApplication1.auto_completeajax" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="AutoComplete.asmx" />
                </Services>
            </asp:ScriptManager>
            <div>
                <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                <cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoComplete.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" CompletionInterval="1" />
            </div>
        </form>
    </body>
    </html>
    

    webservice

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication1
    {
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.Web.Script.Services.ScriptService]
    
        public class AutoComplete : System.Web.Services.WebService
        {
            [WebMethod]
            public string[] GetCountriesList(string prefixText)
            {
                DataSet dtst = new DataSet();
                SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                string strSql = "SELECT coluna1FROM tb_empresa WHERE coluna1 LIKE '" + prefixText + "%' ";
                SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
                sqlCon.Open();
                SqlDataAdapter sqlAdpt = new SqlDataAdapter();
                sqlAdpt.SelectCommand = sqlComd;
                sqlAdpt.Fill(dtst);
                string[] cntName = new string[dtst.Tables[0].Rows.Count];
                int i = 0;
                try
                {
                    foreach (DataRow rdr in dtst.Tables[0].Rows)
                    {
                        cntName.SetValue(rdr["coluna1"].ToString(), i);
                        i++;
                    }
                }
                catch { }
                finally
                {
                    sqlCon.Close();
                }
                return cntName;
            }
        }
    }
    

    • Marcado como Resposta rafa-martin quinta-feira, 29 de novembro de 2012 11:47
    quinta-feira, 29 de novembro de 2012 11:47

Todas as Respostas

  • pq vc não usa o ajax, fiz recentemente um e funciona perfeito olha esse link

    http://simplyasp.blogspot.com.br/2010/07/auto-complete-in-aspnet-c-using-jquery.html


    Junior

    quinta-feira, 29 de novembro de 2012 11:26
  • mais estou usando ajax. de qualquer forma ue já consegui resolver. na verdade eu não tinha setado a propriedade CompletionInterval no objeto autocomplete.ou seja, estava funcionando. porém, demorava para aparecer a lista na tela. abaixo segue código funcionando.

    html

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="auto_completeajax.aspx.cs" Inherits="WebApplication1.auto_completeajax" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="AutoComplete.asmx" />
                </Services>
            </asp:ScriptManager>
            <div>
                <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                <cc1:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoComplete.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" CompletionInterval="1" />
            </div>
        </form>
    </body>
    </html>
    

    webservice

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication1
    {
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.Web.Script.Services.ScriptService]
    
        public class AutoComplete : System.Web.Services.WebService
        {
            [WebMethod]
            public string[] GetCountriesList(string prefixText)
            {
                DataSet dtst = new DataSet();
                SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                string strSql = "SELECT coluna1FROM tb_empresa WHERE coluna1 LIKE '" + prefixText + "%' ";
                SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
                sqlCon.Open();
                SqlDataAdapter sqlAdpt = new SqlDataAdapter();
                sqlAdpt.SelectCommand = sqlComd;
                sqlAdpt.Fill(dtst);
                string[] cntName = new string[dtst.Tables[0].Rows.Count];
                int i = 0;
                try
                {
                    foreach (DataRow rdr in dtst.Tables[0].Rows)
                    {
                        cntName.SetValue(rdr["coluna1"].ToString(), i);
                        i++;
                    }
                }
                catch { }
                finally
                {
                    sqlCon.Close();
                }
                return cntName;
            }
        }
    }
    

    • Marcado como Resposta rafa-martin quinta-feira, 29 de novembro de 2012 11:47
    quinta-feira, 29 de novembro de 2012 11:47