AutoComplete or suggest control for Ajax. net

Source: Internet
Author: 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 wocould 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 shocould work.

Wocould 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 beginningA. You will see a list of Customer names I have stored in SQL Server. HittingTabWill place your cursor in the order number text box. All order numbers are starting0, So try to enter0. You shoshould 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.MYou 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 includedAutoComplete. jsYou 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 abve we are returning a datatable FROM THE METHODNamespcae. classname. ajaxmethod. InGetdisplay ()We have to return the data that will be displayed in the select control, andGetvalue ()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 returnString []You can simply useMs. Web. AutoCompleteClass in JavaScript and only implementGetdata ()Method, that's all.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.