本文主要是介绍在asp.net工程中使用jQuery-ui的autocomplete功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
整理技术核心在于前端html请求调用Handler.ashx页面查询json结果返回给前端页
几乎大部分交互都是基于jQuery在前端完成,而自动完成(autocomplete)的结果需要服务器进行查询,因此在html请求的过程中进行数据交互处理才能实现这个效果
Default.aspx.cs页核心代码
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <script type="text/javascript" src="js/jquery.js"></script>
- <link type="text/css" rel="stylesheet" href="css/jquery-ui.css" />
- <script type="text/javascript" src="js/jquery-ui.js"></script>
- <script type="text/javascript">
- $(function () {
- $("#txtSearch").autocomplete({
- minLength: 1,
- max: 10,
- source: function (request, response) {
- $.ajax({
- type: "POST",
- url: "Handler.ashx?keyword=" + request.term,
- contentType: "application/json; charset=gb2312",
- dataType: "json",
- success: function (data) {
-
-
- response($.map(data, function (item) {
- return { value: item };
- }));
- },
-
-
-
-
-
- });
- }
- });
- });
- </script>
- </head>
- <body style="overflow:hidden;">
- <input id="txtSearch" type="text" class="input.search-query" runat="server" placeholder="请输入井号">
- </body>
- </html>
后端不需要写,单独新建一个文件Handler.ashx,内容如下
- <%@ WebHandler Language="C#" Class="Handler" %>
- using System;
- using System.Web;
- using System.Collections.Generic;
- using System.Web.Script.Serialization;
- using System.Configuration;
- using Oracle.DataAccess.Client;
-
-
- public class Handler : IHttpHandler
- {
-
-
-
-
-
- private string[] GetFilteredList(string keyword)
- {
- List<string> resultList = new List<string>();
- OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["填写要查询的数据库串"].ConnectionString);
- OracleCommand cmd = conn.CreateCommand();
- conn.Open();
- cmd.InitialLONGFetchSize = -1;
- OracleDataReader dr;
- cmd.CommandText = "填写要查询的sql语句注意结果必须为单列";
- dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- resultList.Add(dr[0].ToString());
- }
- dr.Close();
- conn.Close();
-
- int maxlist = 10;
- List<string> filteredList = new List<string>();
- foreach (string sResult in resultList)
- {
-
- if (sResult.Contains(keyword.ToUpper()) && maxlist != 0)
- {
- filteredList.Add(sResult);
- maxlist--;
- }
- }
-
- return filteredList.ToArray();
- }
-
-
- public void ProcessRequest(HttpContext context)
- {
- string keyword = context.Request.QueryString["keyword"];
- if (keyword != null)
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
-
- string jsonString = serializer.Serialize(GetFilteredList(keyword));
- context.Response.Write(jsonString);
- }
- }
-
-
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
-
- }
这篇关于在asp.net工程中使用jQuery-ui的autocomplete功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!