如何在ASP.NET中获取jquery自动完成的所选项的ID

时间:2016-08-13 07:55:32

标签: c# asp.net jquery-ui-autocomplete

在我的jquery自动填充文本框中我想显示"标题"数据库中的值和我的代码工作正常,但我希望当用户单击一个结果时,将其重定向到此页面:

ArticleDetails.aspx?ID=@ID

但是我的代码正在传递"标题"场和 我不知道如何通过" 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>
&#13;
&#13;
&#13;

这是代码隐藏的:

&#13;
&#13;
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;
            }
        }
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案