AutoComplete or Suggest Control for Ajax.NET

來源:互聯網
上載者:User

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!! 

  • Home
  • How to use .NET data types on client-side code?
  • How to add security to your AjaxMethods?
  • How to use the AjaxServerCache attribute?
  • How to access the Session collection?
  • How to write your own AutoComplete textbox?
  • How to create a simple feedback form?
  • How to write IJavaScriptConverters?
  • Special Examples
AutoComplete ExamplePublished: 03.06.2006 10:00:00 Michael Schwarz

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.