利用
Microsoft
的
ajax
工具包开发类似与
Google
搜索的自动提示功能。
可以利用
Ajax
工具包中的
AutoCompleteExtender
。
具体的使用方法:
1.
安装工具包就不用啰唆了;
2. AutoCompleteExtender
控件拖入到窗体中,顺便拖一个
TextBox
3.
设置
AutoCompleteExtender
属性,主要有下面几个·
TargetControlID
:需要绑定的文本框的
ID
ServicePath
:
web service
的文件路径
ServiceMethod
:
web service
的方法·
MinimumPrefixLength
:输入几个字符开始提示
CompletionInterval
:输入字符后多长时间进行提示。默认:
1
秒
EnableCaching
:是否缓存
CompletionSetCount
:列表显示多少行,默认
10
行
4.
书写
web service
代码:看看你的文件是否包含下面这几行代码
[
WebService
(Namespace =
"http://bittime.dyj"
)]
//
命名空间,可以任意一个地址
[
WebServiceBinding
(ConformsTo =
WsiProfiles
.BasicProfile1_1)]
[System.Web.Script.Services.
ScriptService
]
//
用以调用
web service
方法签名
//
很重要的啊
,
如果没有将没什么效果的
5.
书写
webmethod
:下面是我自己写的调用数据库的
Count
:显示下拉中的纪录数;
prefixText
:输入的文本,根据这个和数据库进行匹配。
[
WebMethod
]
public
string
[] GetCompletionList(
string
prefixText,
int
count)
{
if
(count == 0)
{
count = 10;
}
using
(
SqlConnection
connection =
new
SqlConnection
(System.Configuration.
ConfigurationManager
.ConnectionStrings[
"statictis"
].ConnectionString))
{
SqlDataAdapter
sda =
new
SqlDataAdapter
(
"select classname,classpath from ipsum_class where classname like '"
+prefixText+
"%' order by classname"
,connection);
DataSet
ds =
new
DataSet
();
sda.Fill(ds);
List
<
string
> list =
new
List
<
string
>();
for
(
int
i = 0; i < ds.Tables[0].Rows.Count; i++)
{
list.Add(ds.Tables[0].Rows[i][0].ToString());
}
return
list.ToArray();
}
}
按照微软的说法,方法的参数列表(
string,int
),返回值
(string[])
必须这样写,但是按照下面的写法也可以成功不知道怎么回事。
public
string
[] GetCompletionList()
{
string[] s = new string[] { "Hello","Hell"};
return s;
}
另外,我还想完全实现
Google
的功能,不知道写了,谁知道告诉我一下。
1.
输入一个汉字或者字母都可以搜索,而且自动匹配的;
2.
搜索关键词后面的访问量;
3.
下拉框能否进行美化一下。