在asp.net中使用angularjs和Web表单获取数据 [英] fetch data using angularjs and web form in asp.net

查看:113
本文介绍了在asp.net中使用angularjs和Web表单获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html xmlns="http://www.w3.org/1999/xhtml" ng-app="serviceEmp">
<head runat="server">
    <title></title>
    <script src="angular.js"></script>
    <script src="angular.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div ng-controller="empController">
            search:<input type="text" ng-model="search" />
            <table>
                <tr ng-repeat="emp in employees | filter:search">
                    <td>{{emp.EmpId }}
                    </td>
                    <td>{{emp.EmpName}}
                    </td>
                    <td>{{emp.Contact }}
                    </td>
                    <td>{{emp.EmailID }}
                    </td>
                </tr>
            </table>
        </div>
        <script>
            var app = angular.module('serviceEmp', []);
            app.controller('empController', function ($scope, $http) {
                var url = "MY_Service.asmx/HelloWorld";
                $http.get(url).success(function (data) {
                    var myjson = JSON.parse(data);
                    $scope.employees = JSON.parse(myjson);
                })
            });


        </script>
    </form>
</body>
</html>


[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    
    public class MY_Service : System.Web.Services.WebService
    {
        RetriveDataByAngularjs retrive = new RetriveDataByAngularjs();
        [WebMethod]
        //[System.Web.Script.Services.ScriptService]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            string str=    retrive.ConvertDataTableTo_JSON_String(retrive.GetDataTable());
            Context.Response.Write(js.Serialize(str));
           
            return str;
        }
}


public DataTable GetDataTable()
        {
            DataTable dataTable = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from Employee";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dataTable);
            return dataTable;
        }

        public string ConvertDataTableTo_JSON_String(DataTable dataTable)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<String, Object>> tableRows = new List<Dictionary<String, Object>>();
            Dictionary<String, Object> row;
            foreach (DataRow dr in dataTable.Rows)
            {
                row = new Dictionary<String, Object>();
                foreach (DataColumn col in dataTable.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                tableRows.Add(row);
            }
            string str = serializer.Serialize(tableRows);
            return str;
        }

推荐答案

作用域,


http){ var url ="MY_Service.asmx/HelloWorld";
http) { var url = "MY_Service.asmx/HelloWorld";


http.get(url).success(function(data){ var myjson = JSON.parse(data);
http.get(url).success(function (data) { var myjson = JSON.parse(data);


这篇关于在asp.net中使用angularjs和Web表单获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆