我怎么能从javascript读取数据表

how can i read a DataTable from javascript

本文关键字:读取 数据表 javascript 怎么能      更新时间:2023-09-26

我有一个函数,返回一个这样的数据表:

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 ??
     }
      ...

Please help and thank u

试试这个:

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;
}

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

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