问题描述
我试过的信来搜索我的code错误,但我不能让自己的自动完成扩展工作。把招工广告。
下面是我的code:(摘自我的aspx页面)
< ASP:文本框ID =TextBox1的WIDTH =120像素=服务器>< / ASP:文本框>
< CC1:AutoCompleteExtender ID =AutoCompleteExtender1=服务器的TargetControlID =TextBox1的ServiceMethod =GetCompletionListServicePath =SearchAutoComplete.asmx最低prefixLength =1>
< / CC1:AutoCompleteExtender>
我的web code:
[的WebMethod]
公共静态字符串[] GetCompletionList(字符串prefixText,诠释计数)
{
清单<串GT; returnData =新的List<串GT;();
的MySqlConnection CON =新的MySqlConnection(Connection.ConnectionString());
字符串SQL =选择博客的标题,其中标题LIKE'%+ prefixText +%';
MySqlCommand的CMD =新的MySqlCommand(SQL,CON);
con.Open();
MySqlDataReader将读卡器= cmd.ExecuteReader(CommandBehavior.CloseConnection);
而(reader.Read())
{
returnData.Add(读卡器[标题]的ToString());
}
返回returnData.ToArray();
}
还有 GetCompletionList
方法被错误地宣布为静态
,它需要有两个属性; [System.Web.Services.WebMethod]
和 [System.Web.Script.Services.ScriptMethod]
所以,你的声明应该是这样的:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
公共字符串[] GetCompletionList(字符串prefixText,诠释计数){...
此外,您的服务类应具有以下属性:
[WebService的空间(namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[System.ComponentModel.ToolboxItem(假)]
[System.Web.Script.Services.ScriptService]
自动完成扩展也将出现,如果你的 GetCompletionList
方法抛出一个异常被打破。为了防止这种情况,你应该添加周围的code功能的在try..catch
块
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
公共字符串[] GetCompletionList(字符串prefixText,诠释计数)
{
清单<串GT; returnData =新的List<串GT;(); 尝试
{
//数据库访问code,设置returnData
}
赶上(异常前)
{
//记录异常
} 返回returnData.ToArray();
}
I've tried to the letter to search for mistakes in my code, but i can't myself get that autocomplete extender to work. Help wanted.
Here's my code: (excerpt from my aspx page)
<asp:TextBox ID="TextBox1" Width="120px" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ServiceMethod="GetCompletionList" ServicePath="SearchAutoComplete.asmx" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>
My Webservice code:
[WebMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
List<string> returnData = new List<string>();
MySqlConnection con = new MySqlConnection(Connection.ConnectionString());
string sql = "select title from blog where title like '%" + prefixText + "%'";
MySqlCommand cmd = new MySqlCommand(sql, con);
con.Open();
MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
returnData.Add(reader["title"].ToString());
}
return returnData.ToArray();
}
As well as the GetCompletionList
method being incorrectly declared as static
, it needs to have two attributes; [System.Web.Services.WebMethod]
and [System.Web.Script.Services.ScriptMethod]
So your declaration should look like this:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count) { ...
Also your service class should have the following attributes:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
The autocomplete extender will also appear to be broken if your GetCompletionList
method throws an exception. To guard against this you should add a try..catch
block around the code the function
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{
List<string> returnData = new List<string>();
try
{
// database access code that sets returnData
}
catch (Exception ex)
{
// log the exception
}
return returnData.ToArray();
}
这篇关于我怎样才能让我的自动完成扩展工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!