C# 如何使用ajax autocomplete extender使用webservice从数据库填充数据?
在过去的3个小时里,我一直被零件卡住。通过互联网搜索,阅读博客,查找和测试代码和示例,但一无所获 我正在使用textbox上的ajaxcontroltoolkit中的ajaxautocompleteextender,希望根据用户输入的文本从数据库中生成最少的问题 为此,我创建了一个Web服务。Web服务中的方法是-C# 如何使用ajax autocomplete extender使用webservice从数据库填充数据?,c#,asp.net,ajax,web-services,ajaxcontroltoolkit,C#,Asp.net,Ajax,Web Services,Ajaxcontroltoolkit,在过去的3个小时里,我一直被零件卡住。通过互联网搜索,阅读博客,查找和测试代码和示例,但一无所获 我正在使用textbox上的ajaxcontroltoolkit中的ajaxautocompleteextender,希望根据用户输入的文本从数据库中生成最少的问题 为此,我创建了一个Web服务。Web服务中的方法是- namespace CeteraQMS { /// <summary> /// Summary description f
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();
}
}
namespace-CeteraQMS
{
///
///搜索问题的摘要说明
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[ToolboxItem(假)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类搜索问题:System.Web.Services.WebService
{
[网络方法]
[脚本方法]
公共字符串[]GetCompletionList(字符串前缀文本,整数计数)
{
数据集ds=null;
数据表dt=null;
OracleConnection conn=null;
StringBuilder sb=新的StringBuilder();
尝试
{
conn=neworacleconnection(“数据源=advbniit;用户ID=usr;密码=abc providerName=System.Data.OracleClient”);
sb.追加(“从cet中选择issueno,其中issueno类似’”);
某人附加(前缀);
某人加上“%”;
OracleDataAdapter daRes=新的OracleDataAdapter(sb.ToString(),conn);
ds=新数据集();
胆量。填充(ds);
dt=ds.表[0];
}
捕获(异常exc)
{
}
最后
{
if(conn.State==ConnectionState.Open)
{
康涅狄格州关闭();
}
}
List IssueList=新列表();
对于(int i=0;i
我将此Web服务称为-
<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-无错误,无任何事件发生
高于您的[WebMethod]你有什么问题?我也有ajax autocomplete的问题,但我想,我会给你提供我的示例。我用你的表和列替换了我的表和列,但是,你需要在你的sinc上添加一些额外的信息。你有用户名和密码。我没有看到GetRecord表,你包括了吗?你的ajax源代码看起来不错,但是你可以创建一个s如果您愿意,可以使用样式表添加更多首选项
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];
}
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Services;
使用System.Data.SqlClient;
使用系统配置;
使用系统数据;
[网络服务]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
公共类自动完成:WebService
{
公共自动完成()
{
}
[网络方法]
公共字符串[]GetCompletionList(字符串前缀文本,整数计数)
{
如果(计数=0)
{
计数=10;
}
DataTable dt=GetRecords(prefixText);
列表项=新列表(计数);
对于(int i=0;i
在您的[WebMethod]之上你有什么问题?我也有ajax autocomplete的问题,但我想,我会给你提供我的示例。我用你的表和列替换了我的表和列,但是,你需要在你的sinc上添加一些额外的信息。你有用户名和密码。我没有看到GetRecord表,你包括了吗?你的ajax源代码看起来不错,但是你可以创建一个s如果您愿意,可以使用样式表添加更多首选项
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];
}
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Services;
使用System.Data.SqlClient;
使用系统配置;
使用系统数据;
[网络服务]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
公共类自动完成:WebService
{
公共自动完成()
{
}
[网络方法]
公共字符串[]GetCompletionList(字符串前缀文本,整数计数)
{
如果(计数=0)
{
计数=10;
}
DataTable dt=GetRecords(prefixText);
列表项=新列表(计数);
对于(int i=0;i