自动完成填充控件AutoCompleteExtender控件的使用

AutoCompleteProperties的属性包括

属性名称属性描述备注
TargetControlID指定要控制的控件的ID一般为TextBox的ID
ServicePath处理智能选择列表的Web Services路径 
ServiceMethod处理智能选择列表的网络服务服务该方法一般包含两个参数(string prefixText, int count)
Enabled是否可用 
MinimumPrefixLength最小前缀的长度大小当输入长度达到最小的时候,便提供智能选择

一、查看web.config文件中是否存在如下配置(倘若没有请添加): 
 <system.web>
<httpHandlers>
      
<remove verb="*" path="*.asmx"/>
      
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    
</httpHandlers>
 
<system.web>



二、代码如下:

2_SimpleList_AutoComplete_DragandDrop.aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2_SimpleList_AutoComplete_DragandDrop.aspx.cs"
    Inherits
="_2_SimpleList_AutoComplete_DragandDrop" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace
="System.Web.UI" TagPrefix="asp" %>
<%@ 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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:ScriptManager ID="ScriptManager1" runat="server">
                
<Services>
                    
<asp:ServiceReference Path="WebServiceSearchName.asmx" />
                
</Services>
            
</asp:ScriptManager>
            
<asp:TextBox runat="server" ID="TextBoxInput" Width="300" autocomplete="off" />
            
<asp:AutoCompleteExtender runat="server" ID="AutoCompleteExtender1" TargetControlID="TextBoxInput"
                ServicePath
="webservicesearchname.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
                CompletionInterval
="100" EnableCaching="true" CompletionSetCount="12" />
        
</div>
    
</form>
</body>
</html>

添加服务WebServiceSearchName, 且WebServiceSearchName.cs文件在App_Code文件夹下,其实内容如下:

using System;
using System.Web.Services;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
/// Summary description for WebServiceSearchName
/// </summary>

[System.Web.Script.Services.ScriptService]
[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
public class WebServiceSearchName : System.Web.Services.WebService {

    
public WebServiceSearchName () {

        
//Uncomment the following line if using designed components 
        
//InitializeComponent(); 
    }


    [WebMethod]
    
public string[] GetCompletionList(string prefixText, int count)
    
{
        
if (count == 0)
        
{
            count 
= 10;
        }


        Random random 
= new Random();
        List
<string> items = new List<string>(count);
        
for (int i = 0; i < count; i++)
        
{
            
char c1 = (char)random.Next(6590);
            
char c2 = (char)random.Next(97122);
            
char c3 = (char)random.Next(97122);

            items.Add(prefixText 
+ c1 + c2 + c3);
        }


        
return items.ToArray();
    }


}

在这里需要注意以下几点:
   1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:
     [System.Web.Script.Services.ScriptService]
   2.特别需要注意的是GetCompletionList这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
     A.方法的返回类型必需为:string [];
     B.方法的传入参数类型必需为:string  ,   int;
     C.两个传入参数名必需为:prefixText  ,  count。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值