2010-09-16 34 views
1

我們如何根據Google搜索等文本框的輸入過濾結果。 即,如果我輸入「阿拉斯加航空公司」,那麼它會根據我們的輸入過濾並顯示結果。怎麼可能。請幫幫我。感謝advnce ..使用文本框輸入過濾數據

回答

3

如果我理解正確,你來填充結果的任何控制當用戶在輸入框中輸入時需要某種形式的自動完成。

要實現這一目標,您應該使用ajax,而ASP.Net Ajax Toolkit可能就是您正在尋找的。查看http://www.asp.net/ajax/ajaxcontroltoolkit/samples/autocomplete/autocomplete.aspx的示例和文檔。

下面是VS2010樣品並使用ASP.Net工具包4

標記

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> 

<!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:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
     </asp:ToolkitScriptManager> 

     <asp:TextBox runat="server" ID="myTextBox" autocomplete="off" /> 

     <asp:autocompleteextender runat="server" behaviorid="AutoCompleteEx" id="autoComplete1" 
      targetcontrolid="myTextBox" servicepath="AutoComplete.asmx" servicemethod="GetCompletionList" 
      minimumprefixlength="2" completioninterval="1000" enablecaching="true" completionsetcount="20"></asp:autocompleteextender> 

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

AutoComplete.asmx.cs

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class AutoComplete : WebService 
{ 
    public AutoComplete() 
    { 
    } 

    [WebMethod] 
    public string[] GetCompletionList(string prefixText, int count) 
    { 
     if (count == 0) 
     { 
      count = 10; 
     } 
     if (prefixText.Equals("xyz")) 
     { 
      return new string[0]; 
     } 
     Random random = new Random(); 
     List<string> items = new List<string>(count); 
     for (int i = 0; i < count; i++) 
     { 
      char c1 = (char)random.Next(65, 90); 
      char c2 = (char)random.Next(97, 122); 
      char c3 = (char)random.Next(97, 122); 
      items.Add(prefixText + c1 + c2 + c3); 
     } 
     return items.ToArray(); 
    } 
} 
0

在搜索點擊事件綁定網格或者您想通過數據庫查詢使用類似的關鍵字通過將文本框的值作爲輸入參數