我有一个gridview,数据库中有itime_id,日期和分钟。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="hesaplama.aspx.cs" Inherits="hesaplama" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("input[type=checkbox]").change(function () {
var totalminute = 0, ctlPrice;
$('#GridView1 tr').each(function () {
if ($(this).find('input:checkbox').attr("checked")) {
ctlPrice = $(this).find('[id$= lblListPrice]');
totalminute += parseInt(ctlPrice.text().replace(/[^\d\.]/g, ''));
}
$("#<%=GridView1.ClientID %>
[id*=lblTotal]").text(totalminute.toFixed(2));
});
});});
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="itime_id" HeaderText="itime_id" />
<asp:TemplateField HeaderText="date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("date")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server" Text="Total"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="minute">
<ItemTemplate>
<asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("minute")%>'> </asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(Globals.CONNECTION_STRING);
con.Open();
string sql = "SELECT * FROM project p,issue_time t";
MySqlCommand komut = new MySqlCommand(sql, con);
MySqlDataReader okuyucu = komut.ExecuteReader();
GridView1.DataSource = okuyucu;
GridView1.DataBind();
con.Close();
con.Dispose();
}
}
答案 0 :(得分:0)
请尝试使用以下代码段。
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".chkClass").change(function () {
var total = 0;
var chks = $(".chkClass input:checked");
if (chks.length > 0) {
for (var i = 0; i < chks.length; i++) {
total += parseInt($("#" + chks[i].id.replace("CheckBox1", "lblListPrice")).html());
}
}
alert("your checked total minutes is :" + total);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="minute">
<ItemTemplate>
<asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("Minutes")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" CssClass="chkClass" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>