0

我有一个像这样返回 DataTable 的函数:

public DataTable SendOnlineContacts()
{
 ...
    for (int i = 0; i < FriendsDt.Rows.Count; i++)
        {
            int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]);
                DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID);

                if (FriendisOnlineRow.Length > 0)  // friend is online 
                {
                  //  new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]);
                    FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O");
                  }
           }
            return FriendsInfo;
     }

客户端 :

$.ajax({
    type: 'POST',
    url: 'ChatPageTest.aspx/SendOnlineContacts',
    data: '{}',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
     // what to do here to read the DataTable ??
     }
      ...

请帮助并感谢你

4

2 回答 2

1

尝试这个:

public object[][] SendOnlineContacts()
{
    //...
    for (int i = 0; i < FriendsDt.Rows.Count; i++)
    {
        int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]);
        DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID);

        if (FriendisOnlineRow.Length > 0)  // friend is online 
        {
            //  new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]);
            FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O");
        }
    }

    var rows = FriendsInfo.Rows
        .OfType<DataRow>()
        .Select(row => row.ItemArray)
        .ToArray();
    return rows;
}
于 2013-05-13T11:02:29.310 回答
1

您必须定义一种可以读取的格式,因此在将其发送给客户端后JavaScript首先编译为该格式。DataTable在这种情况下,最常见的选择是JSON.

请查看:将 ASP.NET DataTable 转换为 JSON,以在 JavaScript 中使用 DataTable 以获得完整的实现细节。

于 2013-05-13T09:43:15.900 回答