Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bind Dropdown using AngularJS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 397
    Comment on it

    Hi All,

    This log will help you to bind data to dropdown/html select control using AngularJS. Data is populated using webmethod.

    1. <select ng-options="Country.CountryName for Country in CountryList" ng-model="selectedCountry">
    2. </select>

    Where CountryList is the JSON result from server. Country.CountryName -> will bind the country name to select options text and CountryID binds to select options value.

    ASPX page:

    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Binddropdown.aspx.cs" Inherits="Binddropdown" %>
    2.  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4. <html xmlns="http://www.w3.org/1999/xhtml">
    5. <head runat="server">
    6. <title></title>
    7. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    8. </head>
    9. <body>
    10. <form id="form1" runat="server">
    11. <div ng-app="myApp" ng-controller="myCntrl">
    12. <div ng-init="fillCountryList()"></div>
    13. Country List :
    14. <select ng-options="Country.CountryName for Country in CountryList" ng-model="selectedCountry">
    15. </select>
    16.  
    17. <script>
    18. var app = angular.module("myApp", []);
    19. app.controller("myCntrl", function ($scope, $http) {
    20. $scope.CountryList;
    21. $scope.fillCountryList = function () {
    22. var httpreq = {
    23. method: 'POST',
    24. url: 'Binddropdown.aspx/GetCountyList',
    25. headers: {
    26. 'Content-Type': 'application/json; charset=utf-8',
    27. 'dataType': 'json'
    28. },
    29. data: {}
    30. }
    31. $http(httpreq).success(function (response) {
    32. $scope.CountryList = response.d;
    33. alert("Completed.");
    34. })
    35. };
    36. });
    37. </script>
    38. </div>
    39. </form>
    40. </body>
    41. </html>

    C# Code:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Web;
    5. using System.Web.UI;
    6. using System.Web.UI.WebControls;
    7.  
    8. public partial class Binddropdown : System.Web.UI.Page
    9. {
    10. protected void Page_Load(object sender, EventArgs e)
    11. {
    12.  
    13. }
    14.  
    15. [System.Web.Services.WebMethod()]
    16. public static List<CountryList> GetCountyList()
    17. {
    18. List<CountryList> list = new List<CountryList>();
    19. list.Add(new CountryList(1,"India"));
    20. list.Add(new CountryList(2,"USA"));
    21. list.Add(new CountryList(3,"Canada"));
    22. list.Add(new CountryList(4, "Italy"));
    23. return list;
    24. }
    25. }
    26.  
    27. public class CountryList
    28. {
    29. public int CountryID;
    30. public string CountryName;
    31. public CountryList(int _CountryID, string _CountryName)
    32. {
    33. CountryID = _CountryID;
    34. CountryName = _CountryName;
    35. }
    36. }

     

    Happy Coding.....

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: