过去3个小时我一直被困在一边。通过互联网搜索,阅读博客,查看和测试代码和示例,但什么都没有。
我正在使用Ajax Control Toolkit中的Ajax Auto Complete Extender在文本框上工作,并希望根据用户输入的文本从数据库中生成最小的问题。
为此,我创建了一个Web服务。 Web服务中的方法是 -
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>
纯粹而简单。但令我惊讶的是,当我在文本框中输入文本时,没有任何事情发生。 请引导我,好像我哪里出错了。
提前致谢 AKHIL
PS - 没有错误发生任何事情。
答案 0 :(得分: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];
}
}
答案 1 :(得分:0)
在web方法之前添加静态,以使其正常工作......