简体   繁体   English

我怎样才能从javascript中读取DataTable

[英]how can i read a DataTable from javascript

i have a function that return a DataTable like this : 我有一个返回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;
     }

client side : 客户端 :

$.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 请帮忙,谢谢你

You have to define a format you are able to read in JavaScript , so first compile DataTable to that format after send it to a client. 您必须定义一个能够在JavaScript读取的格式,因此在将DataTable发送到客户端之后首先将其编译为该格式。 Most common choice in this case is a JSON . 在这种情况下,最常见的选择是JSON

Please have a look on: Convert ASP.NET DataTable to JSON, to use a DataTable in JavaScript for complete implementation details. 请查看: 将ASP.NET DataTable转换为JSON,在JavaScript中使用DataTable以获取完整的实现细节。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM