使用jQuery与GridView绑定响应

时间:2015-08-11 07:08:59

标签: c# jquery asp.net gridview data-binding

在jQuery函数中,我得到了响应。我想用GridView绑定响应:

jQuery:

    function SrcRpt1() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "HHT_Tracking.aspx/BindDatatable",
            data: "{}",
            dataType: "json",
            function (data) {
               alert(JSON.stringify(data));
            },
            error: function (result) {
                alert("Error");
            }
        });
    }

GridView的:

 <asp:GridView ID="gvReport" runat="server" AllowPaging="True" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="8" CssClass="ContentGridTop" AutoGenerateColumns="False">
 <EditRowStyle CssClass="ContentGridLightRow" />
 <EmptyDataTemplate><center>No Record Found.</center>
 </EmptyDataTemplate>
 <FooterStyle CssClass="ContentGridFooter" />
 <RowStyle CssClass="ContentGridLightRow" />
  <Columns>
   <asp:TemplateField>
      <ItemTemplate>
            <asp:Label ID="lblSNo" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
      </ItemTemplate>
   <HeaderTemplate><center>S No.</center></HeaderTemplate>
   </asp:TemplateField>
        <asp:TemplateField><ItemTemplate>
               <asp:Label ID="lblgvServerIP" runat="server" Text='<%# Bind("serverIP") %>'></asp:Label></ItemTemplate>
        <HeaderTemplate><center>Server IP</center></HeaderTemplate>
                               </asp:TemplateField>
   <asp:TemplateField>
        <ItemTemplate><asp:Label ID="lblgvServerName" runat="server" Text='<%# Bind("serverName") %>'></asp:Label></ItemTemplate>
        <HeaderTemplate><center>Server Name</center></HeaderTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
         <ItemTemplate>
              <asp:Label ID="lblgvJobName" runat="server" Text='<%# Bind("jobName") %>'></asp:Label></ItemTemplate>
         <HeaderTemplate><center>Job Name</center></HeaderTemplate>                                       </asp:TemplateField>
     <asp:TemplateField>
           <ItemTemplate>
           <asp:Label ID="lblgvRunDT" runat="server" Text='<%# Bind("execDate") %>'></asp:Label>
           </ItemTemplate>
           <HeaderTemplate><center>Run DateTime</center></HeaderTemplate>
      </asp:TemplateField>
      <asp:TemplateField>
           <ItemTemplate>
                <asp:Label ID="lblgvRunStatus" runat="server" Text='<%# Bind("runStatus") %>'></asp:Label>
            </ItemTemplate>
            <HeaderTemplate>
                <center>Run Status</center>
            </HeaderTemplate>
       </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
           <asp:Label ID="lblgvDuration" runat="server" Text='<%# Bind("Duration") %>'></asp:Label>
        </ItemTemplate>
        <HeaderTemplate><center>Duration</center></HeaderTemplate>  
     </asp:TemplateField>                     
     <asp:TemplateField>
        <ItemTemplate>      
            <asp:Label ID="lblgvItemIC" runat="server" Text='<%# Bind("recordCount") %>'></asp:Label>            
        </ItemTemplate>          
  <HeaderTemplate><center>Item Inventory Count</center></HeaderTemplate>
  </asp:TemplateField>  
   </Columns>

我正在获取数据中的值。但我不能用gridview绑定记录。

我尝试了以下两种方法,但它无法正常工作。

Method 1:

 success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    $("#gvReport").append("<tr><td>" + data.d[i].ServerIP + "</td><td>" + data.d[i].ServerName + "</td><td>" + data.d[i].JobName + "</td><td>" + data.d[i].ExecDate + "</td><td>" + data.d[i].RunStatus + "</td><td>" + data.d[i].Duration + "</td><td>" + data.d[i].RecordCount + "</td></tr>");
                }                    
            },

Method 2:

      success: OnSuccess,

       function OnSuccess(response) {
        var row = $("[id*=gvReport] tr:last-child").clone(true);
        $("[id*=gvReport] tr").not($("[id*=gvReport] tr:first-child")).remove();
        var count = 1;
        $.each(response.d, function () {
          $("span", row).eq(0).html($(this).find("serverIP").text());
          $("span", row).eq(1).html($(this).find("serverName").text());
          $("span", row).eq(2).html($(this).find("jobName").text());
          $("span", row).eq(3).html($(this).find("execDate").text());
          $("span", row).eq(4).html($(this).find("runStatus").text());
          $("span", row).eq(5).html($(this).find("Duration").text());
          $("span", row).eq(5).html($(this).find("recordCount").text());
          $("[id*=gvReport] tbody").append(row);
          if (count == 1 || (count % 2 != 0)) {
              $(row).css("background-color", "#ffffff");
          }
          else {
              $(row).css("background-color", "#D2CDCD");
          }
          count = count + 1;
          row = $("[id*=gvReport] tr:last-child").clone(true);
      });
    }

请建议。

0 个答案:

没有答案