zoukankan      html  css  js  c++  java
  • AutoComplete or Suggest Control for Ajax.NET


    I have a new demo at http://munich.schwarz-interactive.de/autocomplete.aspx which will use a textbox and Ajax.NET Professional to search for customer names (1st textbox) and then for the orders of this selected customer (2nd textbox).

    There is a big difference to other AutoComplet boxes. You can use any AjaxMethod to do your search. There is an example done with returning DataSets, default would be something like string[] (arrays). The second textbox is using the CustomerID from the first textbox, not the CustomerName. This is also very different to other controls. If you have a look in the source code you will find both controls and how they are rendering the lines.

    I didn't test the control in all browsers, IE and Firefox should work.


    Would be nice to get some feedback!!  ;)

    AutoComplete Example

    Published: 03.06.2006 10:00:00

    Ajax.NET comes without any web control, it is designed only for data exchange with the web server. Here you will see how easy it is to implement a AutoComplete feature.

    This example will include following features:

    • Keyboard support (TAB, ENTER, DOWN, UP, ESC)
    • Mouse support (select on value with the mouse, dblclick in textbox to get list)
    • ParentControl support (change value if parent control has been changed)
    • DataTable built-in support
    • MinChars needed support (default is 0)
    • WaitAfterInput to prevent high request interval to the server

    Please notice that it is only tested on Firefox and Internet Explorer, it may fail on other web browser not because of Ajax.NET.

    Try to enter a cusomter name beginning with A. You will see a list of customer names I have stored in SQL Server. Hitting Tab will place your cursor in the order number text box. All order numbers are starting with 0, so try to enter 0. You should see a order number in bold and maybe one character at the end in red. If you use a customer name starting with i.e. M you will get a different order number selection, it will be text only.

    Customer:

    OrderNumber:

    JavaScript Code

    To use the example AutoComplete feature you need to add a behaviour to the text box control. After you have included the autocomplete.js you can write this:

    <script type="text/javascript" src="scripts/autocomplete.js"></script>
    <script type="text/javascript">
    function init() {
    var x = new MS.Web.AutoCompleteDataTable("searchCustomerID", 10);
    x.getDisplay = function(item) {
    return (item != null ? item.CustomerName : "");
    }
    x.getValue = function(item) {
    return (item != null ? item.CustomerName.toString().trimRight() : "");
    }
    x.getData = function() {
    Namespcae.Classname.AjaxMethod(this.ele.value, this.count, this.callback.bind(this));
    }
    }
    addEvent(window, "load", init);
    </script>

    In the example above we are returning a DataTable from the method Namespcae.Classname.AjaxMethod. In getDisplay() we have to return the data that will be displayed in the select control, and getValue() must return the text that will be put in the text box value after selected one item.

    C# Source Code

    Below you will find the source code used on the server-side Ajax.NET method which will return the list of order numbers for the second text box.

    [AjaxPro.AjaxMethod]
    public DataTable SearchAdvanced(string orderNumber, int customerID, int count)
    {
    DataSet ds = new DataSet();
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["AjaxDemoSqlServer"]);
    SqlCommand cmd = new SqlCommand("SELECT TOP " + count + " * FROM Orders " +
    "WHERE CustomerID = @CustomerID " +
    "AND OrderNumber LIKE @OrderNumber " +
    "ORDER BY OrderNumber, PartNumber, JobNumber", conn);
    cmd.Parameters.AddWithValue("@CustomerID", customerID);
    cmd.Parameters.AddWithValue("@OrderNumber", orderNumber + "%");
    try
    {
    conn.Open();
    try
    {
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    }
    finally
    {
    conn.Close();
    }
    }
    catch (Exception)
    {
    return null;
    }
    return ds.Tables.Count == 1 ? ds.Tables[0] : null;
    }

    As you can see it doesn't matter which arguments you want to use for the search. If you want to return a string[] you can simply use the MS.Web.AutoComplete class in JavaScript and only implement the getData() method, that's all.

  • 相关阅读:
    使用PHP类库PHPqrCode生成二维码
    40+个对初学者非常有用的PHP技巧
    (高级篇)jQuery学习之jQuery Ajax用法详解
    lerna管理前端模块实践
    Node.js:深入浅出 http 与 stream
    从koa-session源码解读session本质
    Elastichsearch实践
    Linux代理搭建TinyProxy
    linux常用命令
    node前后端同构的踩坑经历
  • 原文地址:https://www.cnblogs.com/hhq80/p/708075.html
Copyright © 2011-2022 走看看