测试AutoComplete.asmx 有数据 ,在页面上没有出现自动提示信息
html:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ 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="ajaxToolkit" %>
<!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 id="Head1" runat="server">
<title>Ajax AutoComplete</title>
<style type="text/css">
.autocomplete_completionListElement
{
visibility : hidden;
margin : 0px!important;
background-color : inherit;
color : windowtext;
border : buttonshadow;
border-width : 1px;
border-style : solid;
cursor : 'default';
overflow : auto;
height : 200px;
text-align : left;
list-style-type : none;
}
/* AutoComplete highlighted item */
.autocomplete_highlightedListItem
{
background-color: #ffff99;
color: black;
padding: 1px;
}
/* AutoComplete item */
.autocomplete_listItem
{
background-color : window;
color : windowtext;
padding : 1px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:TextBox ID="TextBox1" runat="server" Width="250px" ></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender
runat="server"
BehaviorID="AutoCompleteEx"
ID="autoComplete1"
TargetControlID="TextBox1"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="1"
EnableCaching="true"
CompletionSetCount="20"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
>
<Animations>
<OnShow>
<Sequence>
<%-- Make the completion list transparent and then show it --%>
<OpacityAction Opacity="0" />
<HideAction Visible="true" />
<%--Cache the original size of the completion list the first time
the animation is played and then set it to zero --%>
<ScriptAction Script="
// Cache the size and setup the initial size
var behavior = $find('AutoCompleteEx');
if (!behavior._height) {
var target = behavior.get_completionList();
behavior._height = target.offsetHeight - 2;
target.style.height = '0px';
}" />
<%-- Expand from 0px to the appropriate size while fading in --%>
<Parallel Duration=".4">
<FadeIn />
<Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteEx')._height" />
</Parallel>
</Sequence>
</OnShow>
<OnHide>
<%-- Collapse down to 0px and fade out --%>
<Parallel Duration=".4">
<FadeOut />
<Length PropertyKey="height" StartValueScript="$find('AutoCompleteEx')._height" EndValue="0" />
</Parallel>
</OnHide>
</Animations>
</ajaxToolkit:AutoCompleteExtender>
</form>
</body>
</html>
AutoComplete.asmx
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
/// <summary>
/// AutoComplete 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
SqlConnection _sqlConnection = new SqlConnection("server=localhost;database=ce;uid=sa;pwd=sa");
SqlDataAdapter da = new SqlDataAdapter("select Words FROM AutoComplete where Words like '" + prefixText + "%'", _sqlConnection);
DataSet ds = new DataSet();
_sqlConnection.Open();
da.Fill(ds);
_sqlConnection.Close();
List<string> items = new List<string>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
items.Add(dr["Words"].ToString());
}
return items.ToArray();
}
}
--------------------编程问答--------------------
<%@ 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>AJAX控件之AutoComplete</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:TextBox ID="MyAuto" runat="server" Width="191px"></asp:TextBox><br />
<br />
<cc1:AutoCompleteExtender ID="ace" runat="server" CompletionInterval="100" MinimumPrefixLength="1"
ServiceMethod="GetCompletionList" ServicePath="WebService.asmx" TargetControlID="MyAuto">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
using System;--------------------编程问答-------------------- 怎么显示的下拉列表 连接在一起了啊! --------------------编程问答-------------------- 参考实例:
using System.Web;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//用以调用Web Service方法签名
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService() { }
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
DataTable dt = new DataTable();
dt = ReturnDataTable("select Words FROM AutoComplete where Words like '" + prefixText + "%'");
List<string> items = new List<string>(count);
for (int i = 0; i < dt.Rows.Count; i++)
{
items.Add(dt.Rows[i][0].ToString());
}
return items.ToArray();
}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "server=localhost;database=ce;uid=sa;pwd=sa";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
}
http://www.cnblogs.com/insus/archive/2011/07/16/2108172.html
补充:.NET技术 , ASP.NET