使用javascript在gridview中找出总数

时间:2015-05-13 07:44:30

标签: javascript asp.net gridview

我想检索已检查的总金额。我正在使用itemtemplate,但我无法检索总金额,请帮助。

<asp:GridView ID="gvTicket"  runat="server" CssClass="table" AutoGenerateColumns="false" OnRowDataBound="gvTicket_RowDataBound">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="nCustomerPk" HeaderText="Id" />
    <asp:BoundField DataField="cTicketNo" HeaderText="Ticket No" />
    <asp:BoundField DataField="dBookDate" HeaderText="Book Date" />
    <asp:BoundField DataField="dDepartDate" HeaderText="Dept. Date" />
    <asp:BoundField DataField="bCancel" HeaderText="Cancel" />
    <asp:BoundField DataField="dCancelDate" HeaderText="Cancel Date" />
    <asp:BoundField DataField="nCancelCharges" HeaderText="Cancel Charge" />
    <asp:BoundField  DataField="nSalesRate"  HeaderText="Amount" />
  </Columns>
</asp:GridView> 

1 个答案:

答案 0 :(得分:1)

GridView将呈现为html。 Javascript将在渲染的html上运行。提供至少部分渲染的html会使这变得更容易,所以下面是让你前进的最佳猜测。

您已经提到过&#39; javascript&#39;但不排除jquery,这使得这样的事情变得如此简单。

我已将其分解为步骤,这可能更短/更清洁(使用map()可以在一行中使用,但如果不是这样,这种方式可能对您有所帮助100%)。

var grid = $("#gvTicket");
var rows = $("tbody>tr", grid);
var tot = 0;
rows.each(function() {
     // >td all td cells that are directly under the row
     // asp.net controls have a horrid tendency to add tables all over the place
     // so you need the > to ensure table>tbody>tr>td (excluding any sub tables)
     var checkCell = $(">td", this)[0]);
     // Look for any controls in checkCell that are :checked (ie the tickbox)
     if ($(":checked", checkCell).length) {
         // Get the cell to sum
         // Wouldn't normally use indexes like this as they're too liable to break
         // it might be possible to add an ID or class to the column and reference that
         var valCell = $(">td", this)[8];
         tot += valCell.text();
     }
});

[0]是带有文本框的第一列 [8]是具有nSalesRate的列

对于任何错误道歉,如果没有引用您的gridview html,这是我的头脑。