簡體   English   中英

Web API的ASP.NET Web表單下拉列表

[英]asp.net web form dropdownlist from web api

我需要如何從Web API(JSON)獲取的數據完全填充下拉列表方面需要一些幫助。 嘗試了多篇文章,但不知何故它不起作用。

我從api獲取的數據:鏈接: http : //192.168.0.11/api/lookup

["Daniel","Mark","John"]

default.aspx網站:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var userrs = $('#userrs');

                $('#btnRefresh').click(function () {
                    $.ajax({
                        type: 'GET',
                        url: 'http://192.168.0.11/api/lookup',
                        dataType: 'json',
                        success: function (data) {
                            userrs.empty();
                            $.each(data, function (val) {
                                var userid = val
                                $("#DropDownList1").append('<option>' + userid + '</option >')

                            });
                        }
                    });
                });
            });
</script>
<body>

    <form id="form1" runat="server" class="auto-style1">
        <asp:Label ID="Label1" runat="server" Text="Select User" CssClass="table"></asp:Label>


        <br/>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="158px">
        </asp:DropDownList>
        <asp:Button ID="btnRefresh" runat="server" OnClick="btnRefresh_Click1" Text="Refresh" />
        <ul id= "userrs"></ul>
        <br />
        <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click1" Text="Send" />
        <br />
        <asp:TextBox ID="txtbox" runat="server" Height="104px" OnTextChanged="txtbox_TextChanged" Width="272px"></asp:TextBox>
        <br />
        </form>
</body>
</html>

目的是通過單擊按鈕btnRefresh將他從api獲得的3個名稱填充到DropDownList1中

由於某種原因,它沒有在下拉列表中添加任何內容。

提前致謝。

您需要一個索引。 (並添加分號。)

$.each(data, function (idx, val) {
    var userid = val;
    $("#DropDownList1").append('<option>' + userid + '</option >');    
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM