类似谷歌,百度的文本框自动提醒

效果:

步骤:

新建网站——添加第三方控件 AjaxControlToolkit.dll——新建页面 —— 添加文本框

单击 添加扩展程序

选择该扩展控件,设置ID。

查看源:

从该属性名称上可以看出: ServicePath 是指服务的位置

好了,新建Web服务

查看该文件及解决方案:

打开App_Code文件下的 HotWords.cs,代码如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services;
 6 
 7 /// <summary>
 8 /// HotWords 的摘要说明
 9 /// </summary>
10 [WebService(Namespace = "http://tempuri.org/")]
11 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
12 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
13 [System.Web.Script.Services.ScriptService]
14 public class HotWords : System.Web.Services.WebService {
15 
16     public HotWords () {
17 
18         //如果使用设计的组件,请取消注释以下行 
19         //InitializeComponent(); 
20     }
21 
22     [WebMethod]
23     public string HelloWorld() {
24         return "Hello World";
25     }
26 
27     /// <summary>
28     /// 查找关键字的方法
29     /// </summary>
30     /// <param name="prefixText">要查找关键字的前缀</param>
31     /// <param name="count">返回几条数据, 未指明默认是10条</param>
32     /// <returns>返回关键字集合</returns>
33      [WebMethod]
34     public string[] getHotWords(string prefixText, int count) {
35          //! 参数类型,参数名和返回类型不要更改。
36 
37         string[] strs = new string[count];
38         for (int i = 0; i < count; i++)
39         {
40             strs[i] = prefixText + i.ToString();
41         }
42         return strs;
43 
44          //此处支持泛型  .toArray();
45     }
46 }
View Code

好了,再次打开前台页面

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoComple.aspx.cs" Inherits="_Default" %><%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <p>
13             <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
14             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
15             <asp:AutoCompleteExtender ID="AutoComple" runat="server" 
16                 Enabled="True" ServicePath="HotWords.asmx" ServiceMethod="getHotWords"
17                 TargetControlID="TextBox1">
18             </asp:AutoCompleteExtender>
19         </p>
20     </form>
21 </body>
22 </html>
View Code


注意着三个属性的值: ServicePath="AutoComple.aspx" ServiceMethod="getHotWords"   TargetControlID="TextBox1",

使用Ajax第三方控件记得在控件前添加  ScriptManager 控件。

浏览一下试试看吧!

-----------------------

这里只是简单接受了该控件的用法。对于学习Ajax的第三方控件,还是建议大家对照示例文件学习。

原文地址:https://www.cnblogs.com/Theladyflower/p/3192821.html