autocomplete控件的作用是根据用户在文本框输入的字符而做出相应的提示效果。
例如google搜索提示功能。
属性列表:
targetcontrolid:要实现提示功能的控件
servicepath:web服务的路径
servicemethod:调用数据使用的方法
completionsetcount:提示数据的行数
minimumprefixlength:用户输入多少字母才出现提示效果
completioninterval:从服务器获取数据的时间间隔,单位为毫秒
enabled:是否启用自动完成功能,默认为true
enablecaching:是否启用缓存
实例解析一、读取数据库实现自动完成功能
autocomplete表:id,name两个字段。
default.aspx代码如下:
head runat=server>
title>autocomplete的使用title>
head>
body>
form id=form1 runat=server>
div>
asp:scriptmanager id=scriptmanager1 runat=server>
asp:scriptmanager>
div>
asp:textbox id=textbox1 runat=server>asp:textbox>
cc1:autocompleteextender id=autocompleteextender1 runat=server targetcontrolid=textbox1 servicepath=webserviceautocomplete.asmx servicemethod=getcompletedepart completionsetcount=2 minimumprefixlength=1
completioninterval=1000>
cc1:autocompleteextender>
form>
body>
webserviceautocomplete.asmx.cs文件代码如下:
using system;
using system.web;
using system.collections;
using system.web.services;
using system.web.services.protocols;
using system.data;
using system.data.sqlclient;
using system.configuration;
..
..
[system.web.script.services.scriptservice]
public class webserviceautocomplete : system.web.services.webservice {
..
..
//定义数组
private static string[] autocompletewordlist = null;
[webmethod]
public string[] getcompletedepart(string prefixtext, int count)
{
//如果数组为空
if (autocompletewordlist == null)
{
dal.db dboperator = new dal.db();
dataset ds = dboperator.getds(select name from autocomplete where name like '+prefixtext+%' order by name);
//填充数组
string[] temp=new string[ds.tables[0].rows.count];
int i = 0;
foreach (datarow dr in ds.tables[0].rows)
{
temp[i] = dr[name].tostring();
i++;
}
//将临时数组的内容赋给返回数组
autocompletewordlist = temp;
}
string[] returnvalue = new string[count];
returnvalue = autocompletewordlist;
//返回
return returnvalue;
}
}
