我想检索已检查的总金额。我正在使用 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>
最佳答案
GridView 将呈现为 html。 JavaScript 将在渲染的 html 上运行。至少提供部分渲染的 html 会让这件事变得更容易,所以下面是帮助您继续前进的最佳猜测。
您提到了“javascript”,但并未排除 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。
关于javascript - 使用javascript找出gridview中的总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30208658/