<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataRow dr; dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); for (int i = 0; i < 8; i++) { System.Random rd = new System.Random(Environment.TickCount * i); ; dr = dt.NewRow(); dr[0] = "班级" + i.ToString(); dr[1] = "【孟子E章】" + i.ToString(); dr[2] = System.Math.Round(rd.NextDouble() * 100, 0); dr[3] = System.Math.Round(rd.NextDouble() * 100, 0); dr[4] = System.Math.Round(rd.NextDouble() * 100, 0); dr[5] = System.Math.Round(rd.NextDouble() * 100, 0); dt.Rows.Add(dr); } System.Data.DataView dv = new System.Data.DataView(dt); GridView1.DataSource = dv; GridView1.DataBind(); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script> function Gen(t) { var tr = t.parentNode; while (tr.tagName != "TR") { tr = tr.parentNode; } if (!tr) { return; } if (tr.tagName != "TR") { return; } if (t.value != "" && !isNaN(parseInt(t.value, 10))) tr.cells[2].getElementsByTagName("span")[0].innerHTML = parseInt(t.value, 10) + parseInt(tr.cells[1].innerHTML) } </script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField><ItemTemplate>输入:<asp:textBox id="t" runat="server" onblur="Gen(this)"></asp:textBox></ItemTemplate></asp:TemplateField> <asp:BoundField DataField="数学" /> <asp:TemplateField> <ItemTemplate>总计:<span></span></ItemTemplate></asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>