asp.net -TextBox文本框输入框输入后如百度等搜索引擎的自动提示、自动完成、自动补全功能-wcf服务实现

和另一篇文章类似,此处用asp.net自带的 wcf服务实现输入框的自动补全,连接数据库取数实时显示匹配内容

1.web画面添加文本框和对应的方法

                                        <asp:TextBox ID="txtSupplier" runat="server"></asp:TextBox>
                                        <cc1:AutoCompleteExtender ID="aceSupplier" runat="server" TargetControlID="txtSupplier" CompletionInterval="1000" 
                                            MinimumPrefixLength="1" ServicePath="WebSupplier.svc" ServiceMethod="GetData">
                                        </cc1:AutoCompleteExtender>

2.新建一个 WebSupplier.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="WebSupplier" CodeBehind="~/App_Code/WebSupplier.cs" %>

3.建立类 WebSupplier.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WebSupplier
{
	// 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
	// 要创建返回 XML 的操作,
	//     请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
	//     并在操作正文中包括以下行:
	//         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

    [OperationContract]
    public string[] GetData(string prefixText, int count)
    {
        return csService.QuerySupplier(prefixText);
    }
	// 在此处添加更多操作并使用 [OperationContract] 标记它们
}

4.上面类调用的另一个类csServiceQuerySupplier方法,该方法返回字符数据,用于在web画面上显示一个个字符:

 

	public static string[] QuerySupplier(string key)
	{
		SqlHelper.AddParameter("@keyword", key);
		string[] result = new string[10];
		SqlDataReader read = SqlHelper.ExecuteReader("stp_query", SqlHelper.GetParameters());
		int i = 0;
		while ((read.Read())) {
            result[i] = read["supName"].ToString();
			i = i + 1;
		}
		string[] result2 = new string[i];
		for (int j = 0; j <= i - 1; j++) {
			result2[j] = result[j];
		}
		return result2;
	}

在测试过程中发现,再某些服务器上会不显示效果, 改成另一个jquery 方法完美解决:

asp.net -TextBox文本框输入框输入后如百度等搜索引擎的自动提示、自动完成、自动补全功能-jquerys实现

 

 

 

 

 

作者: 轻烟随风
当前文章地址: https://www.zyxpp.com/textboxautoindicatewcf/
来源: 轻烟随风的博客
文章版权归作者所有,欢迎转载
THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录