博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net ajax控件工具集 AutoCompleteExtender控件
阅读量:4337 次
发布时间:2019-06-07

本文共 2212 字,大约阅读时间需要 7 分钟。

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

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

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

你可能感兴趣的文章
poj 2368 Buttons
查看>>
HBase参数配置及说明
查看>>
Failed to read artifact descriptor for avalon-framework:avalon-framewor
查看>>
linux下批量将文件由windows格式转换为unix格式
查看>>
接口测试入门
查看>>
排列与组合的一些定理(二)
查看>>
My first python program--填运算符问题的实现
查看>>
Scratch少儿编程系列:(二)界面介绍及相关概念
查看>>
OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
查看>>
3.RabbitMQ 第一个程序
查看>>
java中引入脚本语言例子
查看>>
面试小题
查看>>
用C++和Windows的互斥对象(Mutex)来实现线程同步锁
查看>>
SQL Server 2005 视图
查看>>
Win7 IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的...
查看>>
通过反射(Reflection)实现对ref和out参数在 Portal-Builder 开源门户系统中的调用...
查看>>
老李分享:锁定客户的六大策略:教你如何将切换成本嵌入商业模式 1
查看>>
Android源码服务专家(申明:来源于网络)
查看>>
java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络]
查看>>
《大道至简》阅读笔记03
查看>>