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:
- <asp:TextBox ID="txtLearnCenter" runat="server" Width="188px"></asp:TextBox>
- <cc1:AutoCompleteExtender ID="txtLearnCenter_AutoCompleteExtender" runat="server"
- DelimiterCharacters="" Enabled="True" ServicePath="~/WebService/PymService.asmx"
- ServiceMethod="GetLearnCenterByPym" MinimumPrefixLength="1" CompletionSetCount="15"
- TargetControlID="txtLearnCenter" CompletionListHighlightedItemCssClass="autocomplete_highlightedlistitem"
- CompletionListCssClass="autocomplete_completionlistelement" CompletionListItemCssClass="autocomplete_listitem">
- </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.
- public class PymService : System.Web.Services.WebService
- {
- [WebMethod]
- public string[] GetProfessionalByPym(string prefixText, int count)
- {
- return new Common().GetProfessionalNameByPym(prefixText, count);
- }
-
- [WebMethod]
- public string[] GetLearnCenterByPym(string prefixText, int count)
- {
- return new Common().GetLearnCenterByPym(prefixText, count);
- }
-
- [WebMethod]
- public string[] GetStudentNameByPym(string prefixText, int count)
- {
- return new Common().GetStuNameByPym(prefixText, count);
- }
- }
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:
- JQuery ("# courseName"). autocomplete ("/TaskScoreEnter/GetCourseByPym /",{
- MinChars: 0, // minimum number of response characters
- Max: 10, // The maximum number of returned data records
- AutoFill: false,
- Delay: 400,
- MatchContains: true,
- ScrollHeight: 220,
- DataType: 'json ',
- SelectFirst: false,
- Parse: function (data ){
- Var rows = [];
- If (data = null ){
- Return rows;
- }
- For (var I = 0; I <data. rows. length; I ++ ){
- Rows [rows. length] = {
- Data: data. rows [I]. name,
- Value: data. rows [I]. course_id,
- Result: data. rows [I]. name
- };
- }
- Return rows;
- },
- FormatItem: function (row, I, max ){
- Return row;
- },
- FormatMatch: function (row, I, max ){
- Return row;
- },
- FormatResult: function (row ){
- Return row;
- }
- }). Result (function (event, data, formatted ){
- If (formatted = ""){
- $ ("# CourseName"). val ("");
- $ ("# Hfd_course_id"). val ("");
- $ ("# CourseName"). focus ();
- Alert ("Please select accurately", "prompt information ");
- }
- Else {
- $ ("# Hfd_course_id"). val (formatted );
- }
- });
In the background controller, we want to return a data in Json format, as shown below:
- 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