在asp.net中我们期望输入部分字符后,获取提示符以保证快速获取所需的录入信息。
效果图如下:
下面时AutoCompleteExtender的使用方法:
1.新增asp.net空项目或者在项目基础之上进行修改,.net版本保证在3.5之上,因为用的相关ajax。
2.在aspx文件中所作的修改如下:注意需要添加前缀声明,同时需要导入类库AjaxControlToolkit.dll
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServicePath="~/AutoText.asmx"
ServiceMethod="GetWordList" TargetControlID="TextBox1" MinimumPrefixLength="1"
CompletionSetCount="10" CompletionInterval="100">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
3.新建AutoText.asmx文件,这个文件主要填写相应的过滤方法,如下主要在方法GetWordList()中进行修改:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace WebApplication1
{
/// <summary>
/// AutoText 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class AutoText : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
//序列号
[WebMethod]
public string[] GetWordList(string prefixText, int count)
{
string[] array = new string[5] { "中国abcd1234", "abc343", "美abcdfsdfdddd3", "abcdddd4", "ab2342cdddd5"};
return array;
}
}
}
该类中的GetWordList方法,也就是前台进行调用的方法,其中prefixText参数是文本框中输入的值,count参数是下拉中显示的字符串的个数,该方法需要完善。
源码下载:http://download.csdn.net/detail/lyalei/9081297