【问题标题】:how to use ajax autocomplete extender to fill data from database using webservice?如何使用 ajax 自动完成扩展器使用 web 服务从数据库中填充数据?
【发布时间】:2012-08-01 11:26:41
【问题描述】:

在过去的 3 个小时里,我一直被部分卡住。在互联网上搜索、阅读博客、查看和测试代码和示例,但一无所获。

我正在使用文本框上的 Ajax Control Toolkit 中的 Ajax Auto Complete Extender,并希望根据用户输入的文本从数据库中生成最少的问题。

为此,我创建了一个网络服务。 webservice中的方法是 -

    namespace CeteraQMS
    {
        /// <summary>
        /// Summary description for SearchIssues
        /// </summary>
        [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 SearchIssues : System.Web.Services.WebService
        {        
            [WebMethod]
            [ScriptMethod]
            public string[] GetCompletionList(string prefixText, int count)
        {
            DataSet ds = null;
            DataTable dt = null;
            OracleConnection conn = null;
            StringBuilder sb = new StringBuilder();
            try
            {
                conn = new OracleConnection("Data Source=advbniit; User ID=usr; Password=abc providerName=System.Data.OracleClient");
                sb.Append("select issueno from cet_sepcet where issueno like '");
                sb.Append(prefixText);
                sb.Append("%'");
                OracleDataAdapter daRes = new OracleDataAdapter(sb.ToString(), conn);
                ds = new DataSet();
                daRes.Fill(ds);
                dt = ds.Tables[0];
            }           
            catch (Exception exc)
            {

            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }

            List<string> IssueList = new List<string>();
            for (int i = 0; i < dt.DataSet.Tables[0].Rows.Count; i++)
            {
                IssueList.Add(dt.DataSet.Tables[0].Rows[i][0].ToString());
            }           
            return IssueList.ToArray();
        }
     }

我将此网络服务称为 -

 <asp:TextBox ID="txtIssueNo" runat="server" Width="130px" Style="margin-left: 5px"
                onkeypress="return allowDigit(this);" MaxLength="7"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoCompleteExtender1" EnableCaching="true" BehaviorID="AutoCompleteCities"
                TargetControlID="txtIssueNo" ServiceMethod="GetCompletionList" ServicePath="SearchIssues.asmx"
                MinimumPrefixLength="1" CompletionSetCount="10" runat="server" FirstRowSelected="true">
            </asp:AutoCompleteExtender>

纯粹而简单。但令我惊讶的是,当我在文本框中输入文本时,什么都没有发生。 请引导我,好像我哪里错了。

提前致谢 阿基尔

PS - 没有错误什么都没有发生。

【问题讨论】:

  • 您是否使用 `ScriptService' 属性标记了您的 Web 服务?
  • 是的,我已经添加了。我也更新了我上面的代码

标签: c# asp.net ajax web-services ajaxcontroltoolkit


【解决方案1】:

在你的 [WebMethod] 之上你有什么?我也遇到了 ajax 自动完成的问题,但我想通了,我会为您提供我的示例。我用你的替换了我的表格和列,但是你需要在你的 sinc 上添加一些额外的信息,你有一个用户名和密码。我没有看到 GetRecord 表,您是否将其包括在内?您的 ajax 源代码看起来不错,但您可以随其创建样式表以添加更多首选项。

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


[WebService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
    public AutoComplete()
    {
    }

[WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        if (count == 0)
        {
            count = 10;
        }
        DataTable dt = GetRecords(prefixText);
        List<string> items = new List<string>(count);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string strName = dt.Rows[i][0].ToString();
            items.Add(strName);
        }
        return items.ToArray();
    }

    public DataTable GetRecords(string strName)
    {
        string strConn = ConfigurationManager.ConnectionStrings["ProjectASPConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@issueno", strName); ///What name to place here
        cmd.CommandText = string.Format("Select distinct issueno as issueno from cet_sepcet where issueno like '{0}%'", strName); //what command to write here
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        return objDs.Tables[0];
    }
}

【讨论】:

    【解决方案2】:

    在 webmethod 之前添加一个静态元素,让它工作......

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 1970-01-01
      • 2014-03-14
      • 2021-09-09
      • 1970-01-01
      • 2019-06-09
      • 2011-11-15
      • 1970-01-01
      • 2021-03-09
      相关资源
      最近更新 更多