autocompleteextender 自动完成扩展, 配合textbox使用功能类似现在google中输入搜索字,则在textbox下出来下拉框显示搜索目标中的项目
这个扩展控件需要配合web service使用,所以涉及了点web service的使用(这里只简单谈下,等用熟了再仔细谈下web service的内容)
先介绍下autocompleteextender的几个关键属性:
a,targetcontrolid 这个属性是所有ajaxcontroltoolkit的共同属性,就是扩展目标控件id(官方这么说的吧)
b.completionsetcount 这个属性是设置显示下拉结果的条数 默认为10吧
c.minimumprefixtextlength 这个属性是设置输入几个字符的长度后调用webservice中的方法显示下拉列表
d.servicepath 这个属性设置需要调用的web service路径
e.servicemethod 这个属性设置需要调用的web service中的方法(函数)
f.enablecaching:是否在客户端缓存数据,默认为true
g.completioninterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒
注:如果习惯用可视控件设置属性,则a属性在autocompleteextender中设置,其他属性则设置了targetcontrolid后,在相应的targetcontrol中会多出来个extenders属性中设置,如果习惯手写代码,则在autocompleteextender代码属性中设置。
例子: 1.新建一个页面,加入scriptmanager控件 一个textbox控件 一个autocompleteextender控件
2.新建立一个webservice,添加一个[webmethod]方法
[webmethod]
复制代码 代码如下:
public string[] getstring(string prefixtext, int count){
system.collections.generic.list list = new system.collections.generic.list(count);
system.data.dataset ds = new system.data.dataset();
//这里是我在数据库中取数据的代码 其中sqlhelper类是项目中的取数据基类
//string strsql = string.format(select top {0} name from cengwei where name like '{1}%' order by name,count,prefixtext);
//ds = sqlhelper.query(strsql);
//for (int i = 0; i //{
// list.add(ds.tables[0].rows[i][0].tostring());
//}
for (int i = 0; i {
list.add(prefixtext+i.tostring());
}
return list.toarray();
}
其中:必须在webservice的类上面添加
[system.web.script.services.scriptservice]
示例代码:webservice是在数据库中的一个字段中取数据
页面代码:
复制代码 代码如下:
namespace=crystaldecisions.web tagprefix=cr %>
dropdownextender简单练习
rel=stylesheet type=text/css />
servicemethod=getstring servicepath=autocomplete.asmx targetcontrolid=textbox2>
webservice代码:
using system;
using system.web;
using system.collections;
using system.web.services;
using system.web.services.protocols;
///
/// autocomplete 的摘要说明
///
[webservice(namespace = http://tempuri.org/)]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
//下面是必须的,否则功能无法实现
[system.web.script.services.scriptservice]
public class autocomplete : system.web.services.webservice {
public autocomplete () {
//如果使用设计的组件,请取消注释以下行
//initializecomponent();
}
[webmethod]
public string helloworld() {
return hello world;
}
[webmethod]
public string[] getstring(string prefixtext, int count){
system.collections.generic.list list = new system.collections.generic.list(count);
system.data.dataset ds = new system.data.dataset();
//这里是我在数据库中取数据的代码 其中sqlhelper类是项目中的取数据基类
//string strsql = string.format(select top {0} name from cengwei where name like '{1}%' order by name,count,prefixtext);
//ds = sqlhelper.query(strsql);
//for (int i = 0; i //{
// list.add(ds.tables[0].rows[i][0].tostring());
//}
for (int i = 0; i {
list.add(prefixtext+i.tostring());
}
return list.toarray();
}
}
有哪里不对的地方还请大家指教