How to return the value of an ID when selecting an auto complete record

Asked

Viewed 341 times

2

Good afternoon. Reformulating my doubt. As said I am making a sales site in Asp.net. I tried to create a search engine, like Google and Facebook, that will filter the results. I found that Asp.net would be very complicated, so they gave me the hint of using a jquery component, autocomplete. Using this code below, the autocomplete sends the data to my Handler class, and there it goes filtering and returning me the possible products that the user is researching. My problem is when the user selects one of the records that the autocomplete presents. I need to get the ID of that particular record to redirect to the correct page. The problem is I can’t get this ID. I wonder if the error is in my Handler class, or in jquery. I don’t know if I have to assign my Id to a value for jquery read, and if I don’t know which part to assign.

Here’s the HANDLER code

using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Script.Serialization;

namespace Demo
{
    public class StudentHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string term = context.Request["term"] ?? "";
            List<string> listProds = new List<string>();

            string cs =     ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using(SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("usp_pesquisar_produtos", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@pesquisa",
                Value = term
            });
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while(rdr.Read())
            {
                listProds.Add(rdr["codigo"].ToString());
                listProds.Add(rdr["Descricao"].ToString());
            }
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        context.Response.Write(js.Serialize(listProds));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

}

And the Asp.Net code

  <%@ Page Language="C#" AutoEventWireup="true"
     CodeBehind="WebForm1.aspx.cs"
     Inherits="Demo.WebForm1" %>
   <!DOCTYPE html>
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
    <title></title>
    <script src="jquery-1.11.2.js"></script>
    <script src="jquery-ui.js"></script>
    <link href="jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript">
        $(document).ready(function () {
            $('#txtName').autocomplete({
            source: 'StudentHandler.ashx'
        });
        });
    </script>
    </head>
     <body style="font-family: Arial">
    <form id="form1" runat="server">
        Student Name :
        <asp:TextBox ID="txtName" runat="server">
        </asp:TextBox>
      </form>
     </body>
     </html>
  • When executed returns some error?

  • Then @Fabricio , error does not return me. I do the right search. I am typing and the autocomplete will return me the possible records. But what I’m not able to do, is capture the Id of that particular record, the moment the user clicks on the desired record.

  • Well I don’t know Asp.net, but I think you can merge your code with jquery. This example http://stackoverflow.com/questions/7617373/limit-results-in-jquery-ui-autocomplete may give you a light.

  • "I found that Asp.net would be very complicated". Because it would be too complicated?

  • I would have to use the textChanced event, and from what I saw using only Asp.net it would be much harder to do something like autcomplete.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.