时间:2021-07-01 10:21:17 帮助过:24人阅读
[OperationContract]
public string getSortList()
{
List<string> sorts = new List<string>();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["zhui.pc"].ConnectionString))
{
cn.Open();
SqlCommand cmd = new SqlCommand("select [LastName] from [dbo].[Employees]", cn);
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
foreach (DataRow row in dt.Rows)
{
sorts.Add(row[0].ToString());
}
cn.Close();
}
return string.Join(",", sorts.ToArray());
}
3.在default.aspx文件添加js、css文件:
代码如下:
<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.autocomplete.min.js" type="text/javascript"></script>
<link href="Styles/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
4.在default.aspx的head部分中$().ready(function ())函数中调用webservice获取自动补全的数据,并将数据关联到输入框中。
代码如下:
<script type="text/javascript">
$().ready(function () {
NewsSort.getSortList(OnComplete, OnFailed, null);
function OnComplete(args, context) {
$('#MainContent_searchBox').AutoComplete({
'data': args.split(","),
'itemHeight': 20,
'listDirection': 'down',
'width': 280
}).AutoComplete('show');
}
function OnFailed(args) {
alert("出错了!");
}
});
</script>
5.Finish。