从Web方法映射数组值

时间:2014-03-27 07:17:42

标签: javascript asp.net arrays vb.net

我正在尝试将webmethod返回的数组值映射到javascript中声明的数组中。 webmethod数组是二维的。我使用过单维数组映射。但我已经尝试了以下2D代码,但它不起作用。请让我帮忙。

的webmethod:

<WebMethod()> _
<ScriptMethod()> _
Public Shared Function twoDarr(ByVal para() As Object, ByVal spname As String) As Object(,)
    Dim dsrt As New DataSet, dbacc As New dataaccess, arr(0, 0) As Object
    dsrt = dbacc.retds1(spname, conn, para)
    ReDim arr(dsrt.Tables(0).Rows.Count - 1, dsrt.Tables(0).Columns.Count - 1)
    For i As Integer = 0 To dsrt.Tables(0).Rows.Count - 1
        For j As Integer = 0 To dsrt.Tables(0).Columns.Count - 1
            arr(i, j) = dsrt.Tables(0).Rows(i)(j)
        Next
    Next
    Return arr
End Function

使用Javascript:

    PageMethods.twoDarr(para,"sp_rlplsett",success,error);
    function success(res)
    {
        for(i=0;i<res.length-1:i++)
        {
        dsrp[i]=new Array(res[i].length);
        for(j=0;j<res[i].length-1;j++)
        {
        dsrp[i][j]=res[i][j];
        }
        }
    }

注意:让我更换函数Success()块。 感谢!!!

1 个答案:

答案 0 :(得分:0)

网络方法:

<WebMethod()> _
<ScriptMethod()> _
Public Shared Function twoDarr(ByVal para() As Object, ByVal spname As String) As String(,)
    Dim dsrt As New DataSet, dbacc As New dataaccess, arr(0, 0) As String
    dsrt = dbacc.retds1(spname, conn, para)
    ReDim arr(dsrt.Tables(0).Rows.Count - 1, dsrt.Tables(0).Columns.Count - 1)
    Dim len As Integer = arr.Length
    For i As Integer = 0 To dsrt.Tables(0).Rows.Count - 1
        For j As Integer = 0 To dsrt.Tables(0).Columns.Count - 1
            arr(i, j) = dsrt.Tables(0).Rows(i)(j)
        Next
    Next
    Return arr
End Function

使用Javascript:

var dsrp =[];var ind;var col='your col value';var i;var j;
for (i=0;i<2;i++)
{
var data2=[];
for (j=0;j<col;j++)
{
data2.push(ress[ind]);
ind++;
}
dsrp1.push(data2);
}