You must Sign In to post a response.
  • Category: ASP.NET

    Autocomplete extender using JQuery

    My source code as follows

    <script type="text/javascript">
    $(function () {
    source: function (request, response) {
    url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>',
    data: "{ 'prefix': '" + request.term + "'}",
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
    response($.map(data.d, function (item) {
    return {
    label: item.split('-')[0],
    val: item.split('-')[1]
    error: function (response) {
    failure: function (response) {
    select: function (e, i) {
    minLength: 1

    html xmlns="">
    <head id="Head1" runat="server">

    <form id="form1" runat="server">
    <asp:TextBox ID="txtSearch" runat="server" />
    <asp:HiddenField ID="hfCustomerId" runat="server" />
    <asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" />


    Aspx code as follows

    public static string[] GetCustomers(string prefix)

    List customers = new List();
    using (SqlConnection conn = new SqlConnection())

    SqlConnection strconnection = new SqlConnection("Data Source=GOD-PC;Initial Catalog=Test;Trusted_Connection=true");

    using (SqlCommand cmd = new SqlCommand())
    cmd.CommandText = "select ContactName, CustomerId from customers where ContactName like @SearchText + '%'";
    cmd.Parameters.AddWithValue("@SearchText", prefix);
    cmd.Connection = strconnection;
    using (SqlDataReader sdr = cmd.ExecuteReader())
    while (sdr.Read())
    customers.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
    return customers.ToArray();

    protected void Submit(object sender, EventArgs e)

    string customerName = Request.Form[txtSearch.UniqueID];
    string customerId = Request.Form[hfCustomerId.UniqueID];
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + customerName + "\\nID: " + customerId + "');", true);


    In run mode as follows

    Enter Search Name textbox (Submit button)

    In textbox type the contacname and click the submit button. when i click the submit button the CustomerId will be displayed from the customers table

    but when i click the submit button id is not displayed.

    Please help me what is the problem in my above code.
  • #765033
    Do not try to store customerID in hidden textbox just directly collect it from dropdown list using Request.Form[dropdownlist]
    When you set customerID in dropdownlist, try to put alert() in JQuery and check if it is properly set or not

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

Sign In to post your comments