如何使用隐藏字段选择具有ID /值对的jquery自动完成

时间:2017-11-20 16:55:44

标签: javascript jquery asp.net json autocomplete

我正在尝试使用jquery在asp.net中创建一个自动完成文本框。 我的要求是当用户从自动填充文本框中选择一个值时,数据库中的行ID应该分配给隐藏字段。

我的asp页面是:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script src="../Scripts/jquery.js"></script>
    <script src="../Scripts/jquery-ui.js"></script>
    <link href="../Content/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
        $(document).ready(function () {
            $('#MainContent_txtBuildingName').autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "AutoCompleteService.asmx/GetBuildingName",
                        data: "{'term' : '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) {
                            alert('dataFilter:'+ data);
                            return data;
                        },
                        success: function (data) {
                            alert('Success:' + data);
                            response(data.d);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('textStatus:' + textStatus + 'XMLHttpRequest:' + XMLHttpRequest + 'errorThrown' + errorThrown);
                        }
                    });
                },
                minLength: 2,
                focus: function (event, ui) {
                    $('MainContent_txtBuildingName').val(ui.item.BuildingName);
                    $('hdnfBuildingId').val(ui.item.BuildingID);
                    return false;
                }
            }).data('ui-autocomplete')._renderItem = function (ul, item) {
                return $('<li>').data('ui-autocomplete-item', item).append('<a>' + item.BuildingName + '</a>').appendTo(ul);
            };
        });
    </script>

    <div>
        Name : 
        <asp:TextBox ID="txtBuildingName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSelect" runat="server" Text="Submit" />
        <asp:HiddenField ID="hdnfBuildingId" runat="server" />
    </div>
    <div>

我的服务页面是:

[WebMethod]
        public List<PropBuilding> GetBuildingName(string term)
        {
            string conString = ConfigurationManager.ConnectionStrings["DatabaseConString"].ConnectionString;
            List<PropBuilding> buildingRes = new List<PropBuilding>();
            using (SqlConnection con = new SqlConnection(conString))
            {
                SqlCommand cmd = new SqlCommand("sp_GetDetailsOfBuilding", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@BuildingName", term ?? "");
                cmd.Parameters.Add(param);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    PropBuilding prop = new PropBuilding();
                    prop.BuildingID = Convert.ToInt32(rdr["tc_BuildingID"]);
                    prop.BuildingName = rdr["tc_BuildingName"].ToString();
                    buildingRes.Add(prop);

                }
            }
            return buildingRes.ToList();
        }

我的对象属性文件是:

public class PropBuilding
    {
        public int BuildingID { get; set; }
        public string BuildingName { get; set; }
        public string BuildingCode { get; set; }
        public int NoOfRooms { get; set; }
        public string BuildingAddress { get; set; }

        public PropBuilding()
        {

        }

        public PropBuilding(int BuildlingID, string BuildingName)
        {
            this.BuildingID = BuildlingID;
            this.BuildingName = BuildingName;
        }
    }

服务输出:

enter image description here

页面输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

从您的代码和图片中,我发现您的GetBudingName数据类型是xml。

但是,您的javascript数据类型是json,如下所示

url: "AutoCompleteService.asmx/GetBuildingName",
...
dataType: "json",

<强>更新

type: "POST"更改为type: "GET"