<div class="input-group input-group-sm">
    <asp:TextBox ID="textBox" runat="server" CssClass="form-control" autocomplete="off"></asp:TextBox>
    <span class="input-group-addon">
        <asp:Label ID="label" runat="server"></asp:Label>
    </span>
    <asp:HiddenField ID="hiddenField" runat="server" />
</div>

<%--
<script src="../assets/handlebars.js-master/handlebars.min.js"></script>
<link href="../assets/typeahead.js-master/dist/typeaheadjs.css" rel="stylesheet" />
<script src="../assets/typeahead.js-master/dist/typeahead.bundle.min.js"></script>
--%>

<script type="text/javascript">
    $(function () {
        $('#<%= textBox.ClientID %>').typeahead(
            {
                highlight: true,
                hint: false,
                limit: 5,
                minLength: 1
            },
            {
                display: function (suggestion) {
                    return suggestion.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.RegisterNumber %>;
                },
                source: function (query, syncResults, asyncResults) {
                    $.ajax({
                        url: '../App_Handler/TextBoxCrane.ashx?Type=List&<%= string.Join("=' + query + '&", Eastupright.Crane.BLL.Crane.WhereField.RegisterNumberLike, Eastupright.Crane.BLL.Company.WhereField.CompanyNameLike) %>=' + query,
                        dataType: "JSON",
                        success: function (data) { asyncResults(data); }
                    });
                },
                templates: {
                    suggestion: Handlebars.compile
                        (
                        '<div>'
                            +
                            '<span class=\'font-size-12px\'>{{<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.RegisterNumber %>}}</span>'
                            + '_' +
                            '<span class=\'font-size-12px\'>{{<%= Eastupright.Crane.BLL.BCL.Table.CraneCategory %>.<%= Eastupright.Crane.BLL.CraneCategory.Field.CategoryName %>}}</span>'
                            + '_' +
                            '<span class=\'font-size-12px\'>{{<%= Eastupright.Crane.BLL.BCL.Table.Company %>.<%= Eastupright.Crane.BLL.Company.Field.CompanyName %>}}</span>'
                            +
                        '</div>'
                        )
                }
            })
            .on('typeahead:selected', function (e, suggestion, dataSetName) {
                if (suggestion.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.GUID %> != null) {
                    $('#<%= hiddenField.ClientID %>').val
                        (
                            suggestion.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.GUID %>
                        );
                    $('#<%= label.ClientID %>').text
                        (
                            suggestion.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.RegisterNumber %>
                            + '_' +
                            suggestion.<%= Eastupright.Crane.BLL.BCL.Table.CraneCategory %>.<%= Eastupright.Crane.BLL.CraneCategory.Field.CategoryName %>
                            + '_' +
                            suggestion.<%= Eastupright.Crane.BLL.BCL.Table.Company %>.<%= Eastupright.Crane.BLL.Company.Field.CompanyName %>
                        );
                } else {
                    $('#<%= hiddenField.ClientID %>').val('');
                    $('#<%= label.ClientID %>').text('');
                }
                $('#<%= hiddenField.ClientID %>').change();
            })
            .on('keyup', function () {
                $.ajax({
                    url: '../App_Handler/TextBoxCrane.ashx?Type=Model&<%= Eastupright.Crane.BLL.Crane.WhereField.RegisterNumberEqual %>=' + $('#<%= textBox.ClientID %>').val(),
                    dataType: "JSON",
                    success: function (data) {
                        if (data.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.GUID %> != null) {
                            $('#<%= hiddenField.ClientID %>').val
                                (
                                    data.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.GUID %>
                                );
                            $('#<%= label.ClientID %>').text
                                (
                                    data.<%= Eastupright.Crane.BLL.BCL.Table.Crane %>.<%= Eastupright.Crane.BLL.Crane.Field.RegisterNumber %>
                                    + '_' +
                                    data.<%= Eastupright.Crane.BLL.BCL.Table.CraneCategory %>.<%= Eastupright.Crane.BLL.CraneCategory.Field.CategoryName %>
                                    + '_' +
                                    data.<%= Eastupright.Crane.BLL.BCL.Table.Company %>.<%= Eastupright.Crane.BLL.Company.Field.CompanyName %>
                                );
                        } else {
                            $('#<%= hiddenField.ClientID %>').val('');
                            $('#<%= label.ClientID %>').text('');
                        }
                        $('#<%= hiddenField.ClientID %>').change();
                    }
                });
            });
    });
</script>
 if (context.Request.QueryString["Type"] == "List")
                {
                    List<object> response = new List<object>();
                    {
                        List<Model.Crane> list = BLL.Crane.GetModelList
                            (
                                BLL.BCL.WhereCombine
                                (
                                    BLL.BCL.WhereType.OR,
                                    BLL.Crane.Where(context.Request.QueryString, BLL.BCL.WhereType.OR),
                                    BLL.Crane.Where(CompanyGuidInString: BLL.Company.Select(false, BLL.Company.Field.GUID) + BLL.Company.Where(context.Request.QueryString))
                                )
                                +
                                BLL.BCL.Order
                                (
                                    new KeyValuePair<string, BLL.BCL.OrderType>(BLL.Crane.Field.RegisterNumber, BLL.BCL.OrderType.ASC)
                                )
                            );
                        if (list.Count == 0)
                        {
                            response.Add
                                (
                                    new
                                    {
                                        Company = new Model.Company(),
                                        Crane = new Model.Crane(),
                                        CraneCategory = new Model.CraneCategory()
                                    }
                                );
                        }
                        else
                        {
                            foreach (Model.Crane crane in list)
                            {
                                Model.Company company = BLL.Company.GetModel(BLL.Company.Where(GUIDEqual: crane.CompanyGuid));
                                Model.CraneCategory category = BLL.CraneCategory.GetModel(BLL.CraneCategory.Where(CategoryCodeEqual: crane.CraneCategoryCode));

                                response.Add
                                    (
                                        new
                                        {
                                            Company = company,
                                            Crane = crane,
                                            CraneCategory = category
                                        }
                                    );
                            }
                        }
                    }
                    context.Response.Write(JsonConvert.SerializeObject(response));
                }

                if (context.Request.QueryString["Type"] == "Model")
                {
                    Model.Crane crane = BLL.Crane.GetModel(BLL.Crane.Where(context.Request.QueryString));
                    Model.Company company = BLL.Company.GetModel(BLL.Company.Where(GUIDEqual: crane.CompanyGuid));
                    Model.CraneCategory category = BLL.CraneCategory.GetModel(BLL.CraneCategory.Where(CategoryCodeEqual: crane.CraneCategoryCode));

                    var response = new
                    {
                        Company = company,
                        Crane = crane,
                        CraneCategory = category
                    };
                    context.Response.Write(JsonConvert.SerializeObject(response));
                }