AutoComplete 自动选择
像Google那样的,输入关键词,显示相关的关键词,显示12个
前台代码:
Web服务代码:
前台代码:
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
</cc1:ToolkitScriptManager>
<br />
<div class="demoarea">
<asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
<cc1:AutoCompleteExtender
runat="server"
BehaviorID="AutoCompleteEx"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="2"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="12"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
DelimiterCharacters=";, :"
>
</cc1:AutoCompleteExtender>
</div>
</cc1:ToolkitScriptManager>
<br />
<div class="demoarea">
<asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
<cc1:AutoCompleteExtender
runat="server"
BehaviorID="AutoCompleteEx"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="2"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="12"
CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
DelimiterCharacters=";, :"
>
</cc1:AutoCompleteExtender>
</div>
Web服务代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
List<string> items = new List<string>(count);//泛型
SqlConnection myCon = new SqlConnection("Server=.;uid=sa;pwd=62270106;Database=LJexample");//数据库连接
myCon.Open();//打开数据库连接
SqlCommand myCmd = new SqlCommand("select top " + count + " name from LJ where name like '%" + prefixText + "%' group by name order by name", myCon);
SqlDataReader myDR = myCmd.ExecuteReader();
while (myDR.Read())
{
items.Add(myDR["name"].ToString());
}
myDR.Close();
myCon.Close();//关闭数据库连接
return items.ToArray();
}
}
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
List<string> items = new List<string>(count);//泛型
SqlConnection myCon = new SqlConnection("Server=.;uid=sa;pwd=62270106;Database=LJexample");//数据库连接
myCon.Open();//打开数据库连接
SqlCommand myCmd = new SqlCommand("select top " + count + " name from LJ where name like '%" + prefixText + "%' group by name order by name", myCon);
SqlDataReader myDR = myCmd.ExecuteReader();
while (myDR.Read())
{
items.Add(myDR["name"].ToString());
}
myDR.Close();
myCon.Close();//关闭数据库连接
return items.ToArray();
}
}