0
中選定的jquery自動完成項的ID我想從數據庫中顯示「title」值,我的代碼工作正常,但我希望當用戶單擊一個結果時,重定向他此頁:如何獲取在我的jquery自動完成文本框中的ASP.NET
,但我的代碼正在通過「標題」字段和 我不知道如何從數據庫中通過「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;
}
}
}
}
我編輯了我的代碼,但我得到語法錯誤! 描述:編譯服務此請求所需的資源時發生錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。 編譯器錯誤消息:CS1003:語法錯誤, '' 預期 源錯誤: 第35行:{ 線36:result.Add(新RespObj(){ 線37:標籤:博士[「標題「] .ToString(), Line 38:value:dr [」id「]。ToString() Line 39:}); – Tonto23
你設置了所有的方括號等嗎?你能發佈你的代碼嗎? –
我更新並添加了我的問題中的代碼... – Tonto23