0

我有一个对 webmethod 的 ajax 调用,但调用总是失败。这是下面需要的代码。LetterReportDataStore 类进入数据库并填充并返回一个数据表。

    $(document).ready(function () {
           BindGridView('eastern');

    });

    function BindGridView(r) {
        $.ajax({
            type: "POST",
            url: "AgencyLetterReport.aspx/GetLetterReportData",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert('Success');
                for (var i = 0; i < data.d.length; i++) {
                    $("#grdviewe").append("<tr><td>" + data.d[i].Username +
                                          "</td><td>" + data.d[i].Name + "</td></tr>");
                }
                                },
            error: function () { alert('Failed'); }
        })
    }

    <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetLetterReportData() As LetterReportData()
    Dim region As String = "eastern"
    Dim list As List(Of LetterReportData) = New List(Of LetterReportData)
    Dim dt As DataTable = LetterReportDataStore.LoadGridData(region)

    For Each r As DataRow In dt.Rows
        Dim _data As LetterReportData = New LetterReportData
        _data.Username = r.Item(0)
        _data.Name = r.Item(1)

        list.Add(_data)
    Next
    Return list.ToArray()
End Function
4

1 回答 1

0

我已经想通了。我将我的网络方法更改为以下。我在其中一个数据字段中获得了一些空值,因此我使用 .Tostring 将其设为空字符串。我还将 List 更改为 ArrayList,但我认为 List 也可以。问题是我相信的空值。

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetLetterReportData(ByVal region As String) As ArrayList
    'Dim region As String = "eastern"
    Dim list As ArrayList = New ArrayList
    Dim dt As DataTable = LetterReportDataStore.LoadGridData(region)

    For Each r As DataRow In dt.Rows
        Dim _data As LetterReportData = New LetterReportData
        _data.Username = r.Item(0).ToString
        _data.Name = r.Item(1).ToString

        list.Add(_data)
    Next
    Return list
    'Return region
End Function

这是我的 jquery 调用

$(document).ready(function () {
        $("#tabs").tabs();
        BindGridView('eastern');          
    });

    function BindGridView(r) {
        $.ajax({
            type: "POST",
            url: "AgencyLetterReport.aspx/GetLetterReportData",
            data: "{'region' : '" + r + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                //alert(data.d[0].Username);
                for (var i = 0; i < data.d.length; i++) {
                    $("#grdviewe").append("<tr class='GridviewScrollC1Item'><td>" + data.d[i].Username +
                                          "</td><td>" + data.d[i].Name + "</td>" +
                                          "</tr>");
                }
                $('#grdviewe').gridviewScroll({
                    width: $(window).width() - 200,
                    height: $(window).height() - 200
                });
            },
            error: function () { alert('Error on loading data.'); }
        })
    }

如果您在 .append 调用中注意到这一行,class='GridviewScrollC1Item'。我不得不添加这个,因为 gridview 列在数据绑定上丢失了 css 并使列不与标题对齐。尽管gridview的设置如下。标题 css 保留,但 alterrow 样式和 rowstyle 丢失。我现在正在尝试修复更改行样式。

 <asp:GridView ID="grdviewe" runat="server" Width="100%" ShowHeaderWhenEmpty="True">
     <AlternatingRowStyle BackColor="#EEF2FF" />
     <HeaderStyle CssClass="GridviewScrollC1Header" /> 
     <RowStyle CssClass="GridviewScrollC1Item" />
 </asp:GridView>

希望这对任何人都有帮助!

于 2013-03-08T14:26:55.777 回答