Implement AutoComplete in WebForms and MVC

Source: Internet
Author: User

In previous projects, if there is too much data in the drop-down list, we will make the drop-down list multipage, or change it to autocomplete. Today, let's look at how to implement it.

First, let's look at webforms. We use the AjaxControltoolkit control, and we add an AJAX extension to the text box. 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1J4215F3-0.png "/>

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1J42134c-1.png "/> the code is as follows:

 
 
  1. <asp:TextBox ID="txtLearnCenter" runat="server" Width="188px"></asp:TextBox> 
  2.                     <cc1:AutoCompleteExtender ID="txtLearnCenter_AutoCompleteExtender" runat="server" 
  3.                         DelimiterCharacters="" Enabled="True" ServicePath="~/WebService/PymService.asmx" 
  4.                         ServiceMethod="GetLearnCenterByPym" MinimumPrefixLength="1" CompletionSetCount="15" 
  5.                         TargetControlID="txtLearnCenter" CompletionListHighlightedItemCssClass="autocomplete_highlightedlistitem" 
  6.                         CompletionListCssClass="autocomplete_completionlistelement" CompletionListItemCssClass="autocomplete_listitem"> 
  7.                     </cc1:AutoCompleteExtender> 

What are the specific attributes? You can see them by yourself. I will only introduce this ServicePath and ServiceMethod. ServicePath is the web service address. It can be relative or absolute. ServiceMethod is the method provided by the web service.

 
 
  1. public class PymService : System.Web.Services.WebService  
  2.     {  
  3.         [WebMethod]  
  4.         public string[] GetProfessionalByPym(string prefixText, int count)  
  5.         {  
  6.             return new Common().GetProfessionalNameByPym(prefixText, count);  
  7.         }  
  8.  
  9.         [WebMethod]  
  10.         public string[] GetLearnCenterByPym(string prefixText, int count)  
  11.         {  
  12.             return new Common().GetLearnCenterByPym(prefixText, count);  
  13.         }  
  14.  
  15.         [WebMethod]  
  16.         public string[] GetStudentNameByPym(string prefixText, int count)  
  17.         {  
  18.             return new Common().GetStuNameByPym(prefixText, count);  
  19.         }  
  20.     } 

Note that the parameters must be prefixText and count.

Next let's take a look at how to implement it in mvc. here we need to use the jquery plug-in jquery. autocomplete. js. The Code is as follows:

 
 
  1. JQuery ("# courseName"). autocomplete ("/TaskScoreEnter/GetCourseByPym /",{
  2. MinChars: 0, // minimum number of response characters
  3. Max: 10, // The maximum number of returned data records
  4. AutoFill: false,
  5. Delay: 400,
  6. MatchContains: true,
  7. ScrollHeight: 220,
  8. DataType: 'json ',
  9. SelectFirst: false,
  10. Parse: function (data ){
  11. Var rows = [];
  12. If (data = null ){
  13. Return rows;
  14. }
  15. For (var I = 0; I <data. rows. length; I ++ ){
  16. Rows [rows. length] = {
  17. Data: data. rows [I]. name,
  18. Value: data. rows [I]. course_id,
  19. Result: data. rows [I]. name
  20. };
  21. }
  22. Return rows;
  23. },
  24. FormatItem: function (row, I, max ){
  25. Return row;
  26. },
  27. FormatMatch: function (row, I, max ){
  28. Return row;
  29. },
  30. FormatResult: function (row ){
  31. Return row;
  32. }
  33. }). Result (function (event, data, formatted ){
  34. If (formatted = ""){
  35. $ ("# CourseName"). val ("");
  36. $ ("# Hfd_course_id"). val ("");
  37. $ ("# CourseName"). focus ();
  38. Alert ("Please select accurately", "prompt information ");
  39. }
  40. Else {
  41. $ ("# Hfd_course_id"). val (formatted );
  42. }
  43. });

In the background controller, we want to return a data in Json format, as shown below:

 
 
  1. public JsonResult GetCourseByPym()
            {
                try
                {
                    string pym = Request["q"];
                    string teacherId = (string)Session["userid"];
                    List<TA_Courses> courseList = taskScoreService.GetCourseList(pym, teacherId);
                    var data = new { rows = (from c in courseList select new { c.course_id, c.name }).ToArray() };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return null;
                }
            }

 

Now, we are here.

This article is from the "Microsoft technology" blog, please be sure to keep this source http://leelei.blog.51cto.com/856755/862862

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.