Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Append data in dropdownlist using jQuery in asp.net
Posted

1 solution

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                data: "{}",
                url: "how to append dropdownlist using jquery ajex.aspx/getSelectList",
                dataType: "json",
                success: function(data) {
                    var select = $("#example");
                    select.children().remove();
                    if (data.d) {
                        $(data.d).each(function(index, item) {
                            select.append($("<option>").val(item.Value).text(item.Text));
                        });
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                }
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <select id="example" name="example" multiple="multiple">
    </select>
    </form>
</body>
</html></html>


[WebMethod]
   public static object getSelectList()
   {
       var objList = (new[] { new { Text = "Option 1", Value = 1 } }).ToList();
       objList.Add(new { Text = "Option 2", Value = 2 });
       objList.Add(new { Text = "Option 3", Value = 3 });
       objList.Add(new { Text = "Option 4", Value = 4 });
       objList.Add(new { Text = "Option 5", Value = 5 });
       objList.Add(new { Text = "Option 6", Value = 6 });
       objList.Add(new { Text = "Option 7", Value = 7 });
       return objList;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900