2016-08-13 75 views
0

中選定的jquery自動完成項的ID我想從數據庫中顯示「title」值,我的代碼工作正常,但我希望當用戶單擊一個結果時,重定向他此頁:如何獲取在我的jquery自動完成文本框中的ASP.NET

[email protected]

,但我的代碼正在通過「標題」字段和 我不知道如何從數據庫中通過「ID」域至HREF。

這是我的代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSearch.aspx.cs" Inherits="TestSearch" %> 
 

 
<!DOCTYPE html> 
 

 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head runat="server"> 
 
    <title></title> 
 
    <script src="js/jquery.js"></script> 
 
    <script src="js/jquery-ui.js"></script> 
 
    <link href="css/jquery-ui.css" rel="stylesheet" /> 
 
    <link href="css/site.css" rel="stylesheet" /> 
 
    <script type="text/javascript"> 
 
     $(document).ready(function() { 
 
      SearchText(); 
 
     }); 
 
     function SearchText() { 
 
      $("#SearchText").autocomplete({ 
 
       select: function (event, ui) { 
 
        window.location.href = 'ArticleDetails.aspx?ID=' + ui.item.value; 
 
       }, 
 
       source: function (request, response) { 
 
        $.ajax({ 
 
         type: "POST", 
 
         contentType: "application/json; charset=utf-8", 
 
         url: "TestSearch.aspx/Getauto", 
 
         data: "{'title':'" + document.getElementById('SearchText').value + "'}", 
 
         dataType: "json", 
 
         success: function (data) { 
 
          response(data.d); 
 
         }, 
 
         error: function (result) { 
 
          alert("ERROR!!!"); 
 
         } 
 
        }); 
 
       } 
 
      }); 
 
     } 
 
    </script> 
 
</head> 
 
<body> 
 
    <form id="form1" runat="server"> 
 
    <div> 
 
    <asp:TextBox ID="SearchText" runat="server"></asp:TextBox> 
 
    </div> 
 
    </form> 
 
</body> 
 
</html>

,這是後臺代碼:

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

 
public partial class TestSearch : System.Web.UI.Page 
 
{ 
 
    protected void Page_Load(object sender, EventArgs e) 
 
    { 
 
     SearchText.Text = null; 
 
    } 
 

 
    public class RespObj 
 
    { 
 
     public string label { get; set; } 
 
     public string value { get; set; } 
 
    } 
 

 
    [WebMethod] 
 
    public static List<RespObj> Getauto(string title) 
 
    { 
 
     List<RespObj> result = new List<RespObj>(); 
 
     string cs = ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString; 
 
     using (SqlConnection con = new SqlConnection(cs)) 
 
     { 
 
      SqlCommand cmd = new SqlCommand("SpMySearch", con); 
 
      cmd.CommandType = CommandType.StoredProcedure; 
 
      { 
 
       con.Open(); 
 
       cmd.Parameters.AddWithValue("@SearchText", title); 
 
       SqlDataReader dr = cmd.ExecuteReader(); 
 
       while (dr.Read()) 
 
       { 
 
        result.Add(new RespObj(){ 
 
         label = dr["title"].ToString(), 
 
         value = dr["id"].ToString() 
 
        }); 
 
       } 
 
       return result; 
 
      } 
 
     } 
 
    } 
 
}

回答

0

jQuery用戶界面也應支持,如果你使用帶有屬性標籤和值的Json對象數組。所以在你的服務器的響應中,你必須返回這個對象的Json數組。

也許你改變了服務器代碼是這樣的:

class RespObj { 
    public string label { get; set; } 
    public string value { get; set; } 
} 

[WebMethod] 
public static List<RespObj> Getauto(string title) 
{ 
    List<RespObj> result = new List<RespObj>(); 
    string cs = ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(cs)) 
    { 
     SqlCommand cmd = new SqlCommand("SpMySearch", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     { 
      con.Open(); 
      cmd.Parameters.AddWithValue("@SearchText", title); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       result.Add(new RespObj(){ 
        label = dr["title"].ToString(), 
        value = dr["id"].ToString() 
       }); 
      } 
      return result; 
     } 
    } 
} 

工作需要,可能需要解析Jsonarray客戶端。所以,只要改變這一行就可以了。

response(JSON.parse(data)); 

現在你應該可以訪問價值特性,並獲得相關的ID。

+0

我編輯了我的代碼,但我得到語法錯誤! 描述:編譯服務此請求所需的資源時發生錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。 編譯器錯誤消息:CS1003:語法錯誤, '' 預期 源錯誤: 第35行:{ 線36:result.Add(新RespObj(){ 線37:標籤:博士[「標題「] .ToString(), Line 38:value:dr [」id「]。ToString() Line 39:}); – Tonto23

+0

你設置了所有的方括號等嗎?你能發佈你的代碼嗎? –

+0

我更新並添加了我的問題中的代碼... – Tonto23