如何使用textBox搜索并提供建议列表?

时间:2013-02-10 08:42:57

标签: c# asp.net

我正在开发一个邮件系统,我想通过文本框搜索电子邮件,并在用户输入第一个字母时给出一些建议。

我怎样才能实现这一目标,你能帮助我吗?

2 个答案:

答案 0 :(得分:2)

有几种方法可以实现这一目标。 Here is asp中一个简单的自动完成文本框的示例,但建议您使用jquery及其更简单的autocomplete()函数:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Autocomplete - Default functionality</title>

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
  <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>

  <script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];

    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
</div>

</body>
</html>

答案 1 :(得分:0)

您想使用TextBox.AutoCompleteMode属性。

var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
                {
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                });

//Add datasource to textbox
textbox.AutoCompleteCustomSource = source;
textbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textbox.AutoCompleteSource = AutoCompleteSource.CustomSource;