2013-03-04 116 views
0

我使用jQuery自動完成文本框中輸入一個自定義的jQuery的自動完成的響應消息,因爲我正在輸入姓名與Z或東西是無法在數據庫啓動時,在那個時候我需要顯示在自動完成提的是,「沒有數據單項找到」如何設置

我想出了一個解決方案自定義消息,請糾正我,如果以下提到的解決方法是不正確或任何其他替代方式來實現它。

在此先感謝。

自動完成TextBox.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleteTextBox.aspx.cs" Inherits="Ajax_Using_Jquery.AutoCompleteTextBox" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Ajax Auto Complete Using Jquery</title> 
    <script type="text/javascript" src="JQuery/jquery-1.9.0.mini.js"></script> 
    <script type="text/javascript" src="JQuery/jquery-ui.min.js" ></script> 
    <%--<link type="text/css" href="StyleSheet/jquery-ui.css" rel="Stylesheet" />--%> 
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 


     <%--<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> --%> 
    <script type="text/javascript"> 


    $(document).ready(function() { 

     $(".autosuggest").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: "AutoCompleteFetchService.asmx/getUserNames", 
        data: "{'TextBoxVal':'" + document.getElementById('txtName1').value + "'}", 
        dataType: "json", 
        success: function (data) { 
         response(data.d); 
         if (data.d == "" || data.d == null || typeof (data.d) == 'undefined') { 

          response(["no search found"]); 
         } 
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
         alert("error " + XMLHttpRequest); 
         alert("error " + textStatus); 
         alert("error " + errorThrown); 
        } 
       }); 
      } 
     }); 
    }); 
    </script> 
    <%-- <link href="StyleSheet/jquery-ui.css" type="text/css" rel="Stylesheet" />--%> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <table> 
    <tr> 
    <td> 
    <div> 
    User Name : &nbsp;&nbsp; 

     <input type="text" id="txtName1" class="autosuggest" /> 
    </div> 
    </td> 
    </tr> 
    </table> 
    </form> 
</body> 
</html> 

AutoCompleteFetchService.asmx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace Ajax_Using_Jquery 
{ 
    /// <summary> 
    /// Summary description for AutoCompleteFetchService 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService()] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class AutoCompleteFetchService : System.Web.Services.WebService 
    { 


     [WebMethod] 
     public List<string> getUserNames(string TextBoxVal) 
     { 
      string strCon; 
      List<string> objList = new List<string>(); 
      strCon = ConfigurationManager.ConnectionStrings["EmpNameFetch"].ConnectionString; 
      SqlConnection con = new SqlConnection(strCon); 
      SqlCommand cmd = new SqlCommand(@"select EmpName from newTb2 where EmpName like '%" + TextBoxVal + "%'", con); 
      con.Open(); 
      SqlDataReader objReader = cmd.ExecuteReader(); 

      while (objReader.Read()) 
      { 
       objList.Add(objReader["EmpName"].ToString()); 
      } 

      con.Close(); 
      return objList; 

     } 

    } 
} 
+0

小評論:重寫系列警報 – mozillanerd 2013-03-05 16:28:16

回答

0

以下打印 「0結果」,如果沒有找到結果,並且如果找到結果,打印一個有用的信息給用戶說「點擊或向上/向下箭頭」

消息:{noResults:'0'結果',結果:function() {返回'點擊或箭頭U/D'; }}