C# 自动完成不点火
下面看起来都不错,我提供了3个部件,如果有疑问,服务工作正常C# 自动完成不点火,c#,javascript,C#,Javascript,下面看起来都不错,我提供了3个部件,如果有疑问,服务工作正常 /// <summary> /// Summary description for AutoCompleteService /// </summary> [WebService(Namespace = "http://schemas")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.To
/// <summary>
/// Summary description for AutoCompleteService
/// </summary>
[WebService(Namespace = "http://schemas")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteService : BaseWebService
{
/// <summary>
/// Gets or sets the account finder service.
/// </summary>
public ICheckoutService CheckOutService { get; set; }
/// <summary>
/// Finds all addresses matching either the postcode or suburb given in the prefix text.
/// </summary>
/// <param name="prefixText">The prefix text.</param>
/// <param name="count">The count.</param>
/// <returns>Aray of address details</returns>
[WebMethod]
public string[] FindAddresses(string prefixText, int count)
{
//Check the parameters
if (count == 0) count = 10;
var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText);
return suburbs.Take(count).ToArray();
}
}
//
///AutoCompleteService的摘要说明
///
[WebService(命名空间=”http://schemas")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
公共类自动完成服务:BaseWebService
{
///
///获取或设置帐户查找器服务。
///
公共ICheckoutService CheckOutService{get;set;}
///
///查找与前缀文本中给定的邮政编码或郊区匹配的所有地址。
///
///前缀文本。
///伯爵。
///地址详情一览表
[网络方法]
公共字符串[]FindAddresses(字符串前缀,int计数)
{
//检查参数
如果(计数=0)计数=10;
var=CheckOutService.FindMatchingForPartialAddress(prefixText);
返回郊区。取(计数)ToArray();
}
}
接着是javascript
<script language="javascript">
$(function () {
$("#postcode").autocomplete({
source: "AutoCompleteService.asmx/FindAddresses",
minLength: 1,
select: function (event, ui) {
$(this).val(ui.item.value);
}
});
});
</script>
$(函数(){
$(“#邮政编码”)。自动完成({
资料来源:“AutoCompleteService.asmx/FindAddresses”,
最小长度:1,
选择:功能(事件、用户界面){
$(this.val)(ui.item.value);
}
});
});
然后是文本框
<asp:TextBox runat="server" name="postcode" id="postcode" ></asp:TextBox>
回应
<ArrayOfStringxmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns: xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.forcetechnology.com.au/2009/04/Fulfilment"><string>ABBARIVER,WA,6280</string><string>ABBEY,WA,6280</string><string>ACTONPARK,WA,6280</string>string>ADAMSVALE,WA,6375</string></ArrayOfString>
华盛顿州阿伯里弗,6280华盛顿州阿比,6280ACTONPARK,华盛顿州,6280string>华盛顿州亚当斯维尔,6375
好的,我做了以下修改,谢谢你们为我指出了正确的方向,也为我提供了一个起点,也因为这给了我错误信息,这对我很有帮助,所以修改的内容如下
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] FindAddresses(string prefixText)
{
//Check the parameters
var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText);
return suburbs.ToArray();
}
$(document).ready(function () {
$('[ID$=postcode]').live('keyup.autocomplete', function () {
$(this).autocomplete({
source: function (request, response) {
alert(request.term);
$.ajax({
url: '<%=ResolveUrl("AutoCompleteService.asmx/FindAddresses") %>',
data: "{ prefixText:'" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
},
minLength: 1
});
});
});
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串[]FindAddress(字符串前缀)
{
//检查参数
var=CheckOutService.FindMatchingForPartialAddress(prefixText);
返回郊区。ToArray();
}
$(文档).ready(函数(){
$('[ID$=postcode]').live('keyup.autocomplete',函数(){
$(此)。自动完成({
来源:功能(请求、响应){
警报(请求期限);
$.ajax({
url:“”,
数据:“{prefixText:'”+request.term+“}”,
数据类型:“json”,
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
响应($.map)(数据.d,函数(项){
返回{
标签:item.split('-')[0],
val:item.split('-')[1]
}
}))
},
错误:函数(响应){
警报(response.responseText);
},
故障:功能(响应){
警报(response.responseText);
}
});
},
选择:功能(e,i){
},
最小长度:1
});
});
});
如果自动完成是JQueryUI,并且您希望为自动完成提供一些自定义参数,那么您需要提供一个函数来设置源代码。
这将帮助您定义它
正如@Shadow Wizzard所说,您可能需要确保您拥有正确的id,请尝试以下代码。这对我来说很有用
$(document).ready(function () {
$('[ID$=txtLastname]').live('keyup.autocomplete', function () {
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Resources/WebService.asmx/LastName") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
},
minLength: 1
});
});
$(文档).ready(函数(){
$('[ID$=txtLastname]').live('keyup.autocomplete',function(){
$(此)。自动完成({
来源:功能(请求、响应){
$.ajax({
url:“”,
数据:“{'前缀':'”+request.term+“}”,
数据类型:“json”,
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
响应($.map)(数据.d,函数(项){
返回{
标签:item.split('-')[0],
val:item.split('-')[1]
}
}))
},
错误:函数(响应){
警报(response.responseText);
},
故障:功能(响应){
警报(response.responseText);
}
});
},
选择:功能(e,i){
},
最小长度:1
});
});
Web方法是
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] LastName(string prefix)
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
string connectionstring = CCMMUtility.GetCacheForWholeApplication();
conn.ConnectionString = connectionstring;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select distinct top(10) Lastname from tblusers where " +
"Lastname like '%'+ @SearchText + '%' order by Lastname";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}", sdr["Lastname"]));
}
}
conn.Close();
}
return customers.ToArray();
}
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串[]LastName(字符串前缀)
{
列出客户=新列表();
使用(SqlConnection conn=newsqlconnection())
{
string connectionstring=CCMMUtility.GetCacheforWhoLeapApplication();
conn.ConnectionString=连接字符串;
使用(SqlCommand cmd=new SqlCommand())
{
cmd.CommandText=“从tblusers where中选择不同的前(10)个姓氏”+
“类似于“%”++@SearchText++“%”的Lastname按Lastname排序”;
cmd.Parameters.AddWithValue(“@SearchText”,前缀);
cmd.Connection=conn;
conn.Open();
使用(SqlDataReader sdr=cmd.ExecuteReader())
{
while(sdr.Read())
{
Add(string.Format(“{0}”,sdr[“Lastname”]);
}
}
康涅狄格州关闭();
}
返回客户。ToArray();
}
}
textbox元素的实际ID可能不同,因此jQuery找不到它。在标记中添加ClientIDMode=“Static”
。@ShadowWi