AutoCompleteExtender 自动输入完成

AutoCompleteExtender控件可以帮你自动填写TextBox控件(在数据库中查找).
属性:
TarGetControlID:指定要让"自动输入完成"扩展器要扩展的TextBox控件ID.
ServicePath:Web服务的位置路径.
ServiceMehod:要调用的Web服务的方法名.方法签名如下:

[System.Web.Services.WebMethod]
[System.Web.Script.Service.ScriptMethod]
public string[] GetCompetionList(string prefixText,int count){......}


前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"
             MinimumPrefixLength="1" ServiceMethod="GetProductName" ServicePath="WebService.asmx">
            </cc1:AutoCompleteExtender>
        </div>
    </form>
</body>
</html>

WebService代码:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;//关键程序集引用
using System.Collections.Generic;//关键程序集引用

/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]//一定要添加
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod]
    public string[] GetProductName(string prefixText, int count)
    {
        List<string> suggestions=new List<string>();//声明一泛型集合
        SqlConnection con = new SqlConnection("server=.;database=NorthWind;uid=sa;pwd=;");
        con.Open();
        SqlCommand com = new SqlCommand("select distinct productname from Products where productname like @prefixname order by productname", con);
        com.Parameters.Add("@prefixname",SqlDbType.NVarChar).Value=prefixText + "%";
        SqlDataReader sdr = com.ExecuteReader();
        while (sdr.Read())
        {
            suggestions.Add(sdr.GetString(0));
        }
         sdr.close();
        con.close();
        return suggestions.ToArray();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值