从 C# 检索多个值到 JavaScript
Retrieving multiple values from C# to JavaScript
所以我有一个带有 onclick 的 a
标签:
<a onclick="join(4);">Join</a>
现在当点击 a
标签时,它会按以下顺序调用此代码:
JavaScript函数:
function join(gymID) {
PageMethods.getGymInformation(gymID);
}
C# 方法:
[WebMethod]
public gymData getGymInformation(string gymID)
{
gyms gym = new gyms();
DataTable dt = gym.getNewGymInfo(System.Convert.ToInt32(gymID));
DataRow dr = dt.Rows[0];
return new gymData { name = dr["name"].ToString(), strength = dr["strength"].ToString(), speed = dr["speed"].ToString(), defence = dr["defence"].ToString()};
}
public DataTable getNewGymInfo(int gymID)
{
// This returns a datatable with 1 row
return gymTableApapter.getNewGymInfo(gymID);
}
[Serializable]
public sealed class gymData
{
public string name { get; set; }
public string strength { get; set; }
public string speed { get; set; }
public string defence { get; set; }
}
如您所见,JavaScript join 函数调用 C# 方法,该方法随后检索具有 1 行的 DataTable,然后使用自定义数据类型用要返回的数据填充字符串..
现在我想弄清楚如何获取要在 JavaScript 连接函数中提取的 C# 方法返回的信息?
他们有办法做到这一点吗?
在您的 JavaScript 代码中添加 success/error 回调。它可能看起来像这样:
PageMethods.getGymInformation(gymID, onSuccess, onError);
function onSuccess (result) {
// result contains the returned value of the method
}
function onError (result) {
// result contains information about the error
}
然后在 onSuccess
函数中,您将在 result
变量中获得服务器端方法(gymData
类型)的返回值。届时,您可以在客户端代码中使用它做任何您需要做的事情。
如果函数没有任何适用的重用,您甚至可以直接添加它们:
PageMethods.getGymInformation(gymID,
function (result) {
// success
}, function (result) {
// error
});
所以我有一个带有 onclick 的 a
标签:
<a onclick="join(4);">Join</a>
现在当点击 a
标签时,它会按以下顺序调用此代码:
JavaScript函数:
function join(gymID) {
PageMethods.getGymInformation(gymID);
}
C# 方法:
[WebMethod]
public gymData getGymInformation(string gymID)
{
gyms gym = new gyms();
DataTable dt = gym.getNewGymInfo(System.Convert.ToInt32(gymID));
DataRow dr = dt.Rows[0];
return new gymData { name = dr["name"].ToString(), strength = dr["strength"].ToString(), speed = dr["speed"].ToString(), defence = dr["defence"].ToString()};
}
public DataTable getNewGymInfo(int gymID)
{
// This returns a datatable with 1 row
return gymTableApapter.getNewGymInfo(gymID);
}
[Serializable]
public sealed class gymData
{
public string name { get; set; }
public string strength { get; set; }
public string speed { get; set; }
public string defence { get; set; }
}
如您所见,JavaScript join 函数调用 C# 方法,该方法随后检索具有 1 行的 DataTable,然后使用自定义数据类型用要返回的数据填充字符串..
现在我想弄清楚如何获取要在 JavaScript 连接函数中提取的 C# 方法返回的信息?
他们有办法做到这一点吗?
在您的 JavaScript 代码中添加 success/error 回调。它可能看起来像这样:
PageMethods.getGymInformation(gymID, onSuccess, onError);
function onSuccess (result) {
// result contains the returned value of the method
}
function onError (result) {
// result contains information about the error
}
然后在 onSuccess
函数中,您将在 result
变量中获得服务器端方法(gymData
类型)的返回值。届时,您可以在客户端代码中使用它做任何您需要做的事情。
如果函数没有任何适用的重用,您甚至可以直接添加它们:
PageMethods.getGymInformation(gymID,
function (result) {
// success
}, function (result) {
// error
});