我正在尝试创建一个类似谷歌的搜索框。我没有得到任何结果。我只是想暂时使自动完成功能搜索事情尚未完成。以下是我尝试过的。
<====== Directory.aspx 文件 =======>
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<div class="search-box">
<span class="strong">Search Members: </span>
<ajaxToolkit:AutoCompleteExtender ID="autoComplete1" runat="server"
EnableCaching="true"
BehaviorID="AutoCompleteEx"
MinimumPrefixLength="2"
TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
CompletionInterval="100"
CompletionSetCount="20"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
DelimiterCharacters=";, :"
ShowOnlyCurrentWordInCompletionListItem="true">
<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>
<asp:TextBox ID="myTextBox" AutoCompleteType="FirstName" placeholder="Type First Name Here" runat="server"></asp:TextBox>
</div>
和 AutoComplete.asmx.cs 文件
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
///<summary>
/// Summary description for AutoComplete
///</summary>
[WebService(Namespace = "localhost")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
// uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
public AutoComplete()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
//ADO.Net
MySqlConnection cn = new MySqlConnection("Database=bvshree;Data Source=localhost;User Id=root;Password=axcva4@@@3");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
//Compare String From Textbox(prefixText) AND String From
//Column in DataBase(CompanyName)
//If String from DataBase is equal to String from TextBox(prefixText)
//then add it to return ItemList
//-----I defined a parameter instead of passing value directly to
//prevent SQL injection--------//
cmd.CommandText = "select * from family_header Where Self_Name like @myParameter";
cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");
cn.Open();
cmd.ExecuteNonQuery();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
cn.Close();
dt = ds.Tables[0];
//Then return List of string(txtItems) as result
List<string> txtItems = new List<string>();
String dbValues;
foreach (DataRow row in dt.Rows)
{
//String From DataBase(dbValues)
dbValues = row["Self_Name"].ToString();
dbValues = dbValues.ToLower();
txtItems.Add(dbValues);
}
return txtItems.ToArray();
}
}