如何使用jquery,json,angularjs从sql获取表数据而不使用web方法

时间:2015-11-27 10:07:20

标签: jquery angularjs json

我想从SQL获取表数据。当用户单击按钮时,它会从下拉选择的数据中显示所选数据表。我的下拉工作成功,但我没有选中表

 public static List<User> getdata()
       {
           //string Rolename = Request.QueryString["RoleName"];
           string strConnection = "Data Source=192.168.1.42,1433;Initial Catalog=Harneedi;User ID=chaitanya_t;Password=makrotech";

           List<User> userobj1 = new List<User>();
           DataTable dt = new DataTable();
           SqlConnection con = new SqlConnection(strConnection);
           con.Open();
           SqlCommand cmd = new SqlCommand("select userName,[RoleName],[status] from HN_Users_Details as t1 inner join HN_Master_User_Role as t2 on  t1.RoleID=t2.RoleID where RoleName='Administrator'", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   User userinfo = new User();
                   userinfo.UserName = dt.Rows[i]["UserName"].ToString();
                   userinfo.RoleName = dt.Rows[i]["RoleName"].ToString();
                   userinfo.status = dt.Rows[i]["status"].ToString();
                   userobj1.Add(userinfo);
               }
           }

           return userobj1;
       }

var app = angular.module("myApp", []);
    app.controller("myCntrl", function ($scope, $http) {
                    $scope.click = function () {
                                        $.ajax({
                                            method: 'POST',
                                            url: 'WebForm1.aspx?Meth=PD',
                                            success: function (data) {
                                                var table = '<table border=2>';
                                                table += "<tr><th>UserName</th><th>RoleName</th><th>status</th></tr>";
                                                $.each(data, function (index, data) { table += '<tr><td>' + data.UserName + '</td><td>' + data.RoleName + '</td><td>' + data.status + '</tr>'; });
                                                table += '</table>';
                                                $("#dvTable").html(table);
                                            }
                                         });                                                                                    
                                    }
                                });

 <input id="Button1" type="button" class="button" value="button" ng-click="click()" />

1 个答案:

答案 0 :(得分:0)

将该代码替换为此代码

$scope.click = function () {
$.ajax({
 method: 'POST',
url: 'WebForm1.aspx?Meth=PD&name=' + $scope.dropdown,
success: function (data) {
var table = '<table border=2>';
table += "<tr><th>UserName</th><th>RoleName</th><th>status</th></tr>";
$.each(data, function (index, data) { table += '<tr><td>' + data.UserName + '</td><td>' + data.RoleName + '</td><td>' + data.status + '</tr>'; });table += '</table>'; $("#dvTable").html(table);
                                            }
                                         });                                                  
                                    }