Populate jQuery Autocomplete TextBox based on another in ASP.Net using C# and VB.Net

lingers
 
on Mar 22, 2021 06:37 PM
Sample_186129.zip
1400 Views

Autocomplete value in one Textbox to select value from 2nd table into another textbox Autocomplete.

I want to use the autocompleted pid value in txtSearch to slect price from another table and autocomplete in txtSearch1.

that is select price from Sales (Another Table) where pid= textBox1.Text (Autoompletes in textbox2).

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
    $(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default11.aspx/GetAutoCompleteData",
                    data: "{'pid':'" + request.term + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
                $('#lblUserId').text(ui.item.val);
            }
        });
    }

    function SearchText1() {
        $(".autosuggest1").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default11.aspx/GetAutoCompleteData1",
                    data: "{'price':'" + request.term + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
                $('#lblUserId').text(ui.item.val);
            }
        });
    }
</script>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <asp:TextBox ID="txtSearch" class="autosuggest" runat="server" Width="134px"></asp:TextBox>
            <asp:TextBox ID="txtSearch1" class="autosuggest1" runat="server" Width="134px"></asp:TextBox>
        </div>
    </form>
</body>
</html>

 

public partial class Default11 : System.Web.UI.Page
{
    [WebMethod]
    public static List<string> GetAutoCompleteData(string pid)
    {
        List<string> result = new List<string>();
        using (SqlConnection con = new SqlConnection("Data Source=NERE\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=kaging;"))
        {
            using (SqlCommand cmd = new SqlCommand("select id,pid from job where pid LIKE '%'+@SearchText+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@SearchText", pid);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(string.Format("{0}/{1}", dr["pid"], dr["id"]));
                }
                return result;
            }
        }
    }
    [WebMethod]
    public static List<string> GetAutoCompleteData1(string price)
    {
        // i want to use pid in GetAutoCompleteData1 to select price autocomplete here
        List<string> result = new List<string>();
        using (SqlConnection con = new SqlConnection("Data Source=NERE\\SQLEXPRESS01;Integrated Security=true;Initial Catalog=kaging;"))
        {
            using (SqlCommand cmd = new SqlCommand("select id,price from sales where price LIKE '%'+@SearchText1+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@SearchText1", price);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(string.Format("{0}/{1}", dr["price"], dr["id"]));
                }
                return result;
            }
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 24, 2021 12:43 AM