我有一个返回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 ??
}
...
请帮助,谢谢你
答案 0 :(得分:1)
您必须在JavaScript
中定义一个能够阅读的格式,因此在将其发送到客户端之后首先将DataTable
编译为该格式。在这种情况下,最常见的选择是JSON
。
请查看:Convert ASP.NET DataTable to JSON, to use a DataTable in JavaScript了解完整的实施细节。
答案 1 :(得分: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;
}
答案 2 :(得分:0)