我想在文本框中使用AutoCompleteExtender来自动完成用户从mysql数据库的输入。
但是当我运行代码并输入垃圾时的问题应该调用webservice方法,但是服务方法在这里没有返回任何问题的问题是我的代码所以请指教。
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<div>
<asp:UpdatePanel id="UPSearch" runat="server">
<ContentTemplate>
<asp:Panel runat="server" style="position:absolute; top: 48px; left: 10px;">
<asp:TextBox ID="txtSearch" runat="server"
style="position:absolute; top: 17px; left: 235px;"></asp:TextBox>
<cc1:AutoCompleteExtender ID="ACE_txtSearch" runat="server" TargetControlID="txtSearch" ServiceMethod= "GetByLastName" ServicePath="SearchService.asmx" MinimumPrefixLength="1" >
</cc1:AutoCompleteExtender>
<asp:RadioButton ID="rdoLastName" runat="server"
style="position:absolute; top: 96px; left: 224px; width: 186px;"
Text="Search By Last Name"/>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
namespace Imam_Contacts
{
[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 SearchService : System.Web.Services.WebService
{
[WebMethod]
public string[] GetByLastName(string prefixText)
{
int count = 10;
string sql = "Select * from contact_info Where Last_Name like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql,"server=localhost;User Id=root;database=contacts;Persist Security Info=True");
da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["Last_Name"].ToString(), i);
i++;
}
return items;
}
}
}
答案 0 :(得分:0)
这个答案只有在你找不到更好的解决方案时才有意义,但我强烈建议你使用jQuery和它提供的众多自动完成插件之一。它更可靠,更可定制。
答案 1 :(得分:0)
尝试将“ScriptMethod”属性添加到“GetByLastName”方法中。另外,我认为自动完成要求服务方法有第二个参数或类型int,我相信它代表该方法应该返回的最大结果数。
以下是更新后的代码:
namespace Imam_Contacts
{
[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 SearchService : System.Web.Services.WebService
{
[System.Web.Script.Services.ScriptMethod]
[WebMethod]
public string[] GetByLastName(string prefixText, int count)
{
int count = 10;
string sql = "Select * from contact_info Where Last_Name like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql,"server=localhost;User Id=root;database=contacts;Persist Security Info=True");
da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["Last_Name"].ToString(), i);
i++;
}
return items;
}
}
}