asp.net ajax控件工具集 AutoCompleteExtender控件

本文介绍如何使用ASP.NET Ajax AutoCompleteExtender控件实现类似Google搜索关键字提示的功能。通过VS2008和.NET3.5SP1环境,结合SQL Server数据库,演示了从数据库中动态获取匹配建议并显示在文本框下方的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们在搜索框输入关键字的时候,Google会自动列出相关关键字提示。用asp.net Ajax AutoCompleteExtender控件实现

运行环境行vs 2008 .net 3.5sp1   需单独安装ajax控件工具集
demo源码下载

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ 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.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:ScriptManager ID="ScriptManager1" runat="server" />
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
            TargetControlID
="TextBox1" ServiceMethod="GetCompletionList"  CompletionSetCount="10" MinimumPrefixLength="2" EnableCaching="true"
            UseContextKey
="True">
        
</cc1:AutoCompleteExtender>

        
    
    
</div>
    
</form>
</body>
</html>

对应的cs代码文件
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    [System.Web.Services.WebMethodAttribute(),
    System.Web.Script.Services.ScriptMethodAttribute()]
    
public static string[] GetCompletionList(string prefixText, int count,
       
string contextKey)
    {
        SqlConnection conn;
        SqlCommand cmd;
        
string cmdString =
           
"Select CompanyName from Customers WHERE CompanyName LIKE '" +
           prefixText 
+ "%'";
        conn 
= new SqlConnection(@"Data Source=.;DataBase=Northwind;UID=sa;PWD=sa;");
        
// Put this string on one line in your code
        cmd = new SqlCommand(cmdString, conn);
        conn.Open();

        SqlDataReader myReader;
        List
<string> returnData = new List<string>();

        myReader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);

        
while (myReader.Read())
        {
            returnData.Add(myReader[
"CompanyName"].ToString());
        }

        
return returnData.ToArray();
    }

}

转载于:https://www.cnblogs.com/honghu3000/archive/2009/01/08/1371968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值