如何使用Ajax的自动完成扩展使用网络服务来填补数据库中的数据? [英] how to use ajax autocomplete extender to fill data from database using webservice?

查看:187
本文介绍了如何使用Ajax的自动完成扩展使用网络服务来填补数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持为近3时间的一部分。搜索在互联网上,阅读博客,寻找和测试codeS和例子,但一无所获。

我米用Ajax自动完成扩展的文本框工作从Ajax控件工具包,并希望根据用户输入的文本产生的问题,从数据库中免得。

有关这一点,我已经创建了一个Web服务。在Web服务的方法是 -

 命名空间CeteraQMS
    {
        ///<总结>
        ///为SearchIssues摘要说明
        ///< /总结>
        [WebService的空间(namespace =htt​​p://tempuri.org/)]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
        [的ToolboxItem(假)]
        //要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
        [System.Web.Script.Services.ScriptService]
        公共类SearchIssues:System.Web.Services.WebService
        {
            [的WebMethod]
            [ScriptMethod]
            公共字符串[] GetCompletionList(字符串prefixText,诠释计数)
        {
            数据集DS = NULL;
            DataTable的DT = NULL;
            康涅狄格州的OracleConnection = NULL;
            StringBuilder的SB =新的StringBuilder();
            尝试
            {
                康恩=新的OracleConnection(数据源= advbniit;用户ID = USR;密码= ABC的providerName = System.Data.OracleClient的);
                sb.Append(从cet_sepcet选择issueno哪里issueno像');
                sb.Append(prefixText);
                sb.Append(%');
                OracleDataAdapter敢于=新OracleDataAdapter(sb.ToString(),康涅狄格州);
                DS =新的DataSet();
                daRes.Fill(DS);
                dt的= ds.Tables [0];
            }
            赶上(例外EXC)
            {            }
            最后
            {
                如果(conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }            清单<串GT; IssueList =新的List<串GT;();
            的for(int i = 0; I< dt.DataSet.Tables [0] .Rows.Count;我++)
            {
                IssueList.Add(dt.DataSet.Tables [0] .Rows [I] [0]的ToString());
            }
            返回IssueList.ToArray();
        }
     }

我打电话此WebService作为 -

 < ASP:文本框ID =txtIssueNo=服务器WIDTH =130px风格=保证金左:5像素
                安其preSS =返回allowDigit(本); MAXLENGTH =7>< / ASP:文本框>
            < ASP:AutoCompleteExtender ID =AutoCompleteExtender1EnableCaching =真BehaviorID =AutoCompleteCities
                的TargetControlID =txtIssueNoServiceMethod =GetCompletionListServicePath =SearchIssues.asmx
                最低prefixLength =1CompletionSetCount =10=服务器FirstRowSelected =真>
            < / ASP:AutoCompleteExtender>

纯粹而简单。但让我吃惊的什么也没有发生在我的文本框,当我输入文本。
请带我好像在哪里要去错了。

在此先感谢
AKHIL


  

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



解决方案

你上面的[的WebMethod]你有什么办法呢?我曾与AJAX自动完成的问题太多,但我想通了,生病为您提供我的例子。我取代我的表和列与你,但是,你需要添加你的正弦一些额外的信息,你有一个用户名和密码。我没有看到一个GetRecord表,你有吗?在Ajax源$ C ​​$ C看起来不错,但您可以创建一个样式表与它一起添加更多的preferences如果你喜欢。

 使用系统;
使用System.Collections.Generic;
使用System.Web.Services;
使用System.Data.SqlClient的;
使用System.Configuration;
使用System.Data这;
[网络服务]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[System.Web.Script.Services.ScriptService]
公共类自动完成:WebService的
{
    公开自动完成()
    {
    }[的WebMethod]
    公共字符串[] GetCompletionList(字符串prefixText,诠释计数)
    {
        如果(计数== 0)
        {
            数= 10;
        }
        DataTable的DT = GetRecords(prefixText);
        清单<串GT;项目=新的List<串GT;(计数);
        的for(int i = 0; I< dt.Rows.Count;我++)
        {
            字符串则strName = dt.Rows [I] [0]的ToString();
            items.Add(则strName);
        }
        返回items.ToArray();
    }    公开数据表GetRecords(字符串则strName)
    {
        字符串创建失败= ConfigurationManager.ConnectionStrings [ProjectASPConnectionString]的ConnectionString。
        SqlConnection的CON =新的SqlConnection(创建失败);
        CMD的SqlCommand =新的SqlCommand();
        cmd.Connection = CON;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue(@ issueno,则strName); ///什么名字放在这里
        cmd.CommandText =的String.Format(SELECT DISTINCT issueno从cet_sepcet那里issueno像'{0}%'issueno,则strName); //在这里写了什么命令
        数据集objDs =新的DataSet();
        SqlDataAdapter的dAdapter =新的SqlDataAdapter();
        dAdapter.SelectCommand = CMD;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        返回objDs.Tables [0];
    }
}

I have been stuck with part for past 3 hours. searching over the internet, reading blogs, looking and testing codes and examples but got nothing.

I m working with Ajax Auto Complete Extender from Ajax Control Toolkit on textbox and want to generate the lest of issues from the database based on the text inputted by the user.

For this i have created a webservice. The method in the webservice is -

    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();
        }
     }

I am calling this webservice as -

 <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>

Pure and simple. But to my surprise nothing is happening when I input text in my textbox. Please lead me as if where I m going wrong.

Thanks in advance Akhil

PS - No error nothing is happening.

解决方案

Above your [WebMethod] what do you have? I had issues with ajax autocomplete too but I figured out, Ill provide you with my example. I replaced my table and column with yours but, you would need to add some extra info on your sinc you have a username and password. I dont see a GetRecord table, did you include it? Your ajax source code looks fine, but you can create a style sheet along with it to add more preferences if you like.

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];
    }
}

这篇关于如何使用Ajax的自动完成扩展使用网络服务来填补数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆