首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

<asp:GridView>中有<asp:TextBox>控件自动合计,该如何处理

2012-05-20 
asp:GridView中有asp:TextBox控件自动合计在asp:GridView中有5列asp:TextBox控件,第5列“总分”是前

<asp:GridView>中有<asp:TextBox>控件自动合计
在<asp:GridView>中有5列<asp:TextBox>控件,第5列“总分”是前面4列的和。有没有方法,能够根据前面4列输入的数值,自动合计得到总分?
我在后台的cs文件中写了方法,在前面4列中输入数值,页面都要刷新,感觉不好,有没有不刷新页面的方法啊,比如javascript的方法。
aspx代码如下:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
  <table style="width:90%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td align="left">
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;人员绩效考评表
  <asp:Button ID="Button1" runat="server" Text="保 存" onclick="btnSave_Click" />
  <asp:Label ID="lblMessage" runat="server" ForeColor="Red" Font-Size="12px"></asp:Label>
  </td>
  </tr>
  <tr>
  <td align="center">
  <asp:GridView ID="gvPerEva" runat="server" AllowPaging="True" AllowSorting="True" 
  AutoGenerateColumns="False" BackColor="White" PageSize="250" 
  BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="2" 
  GridLines="Horizontal" OnPageIndexChanging="gvPerEva_PageIndexChanging">
  <AlternatingRowStyle BackColor="#F7F7F7"/>
  <Columns>
  <asp:BoundField DataField="工号" HeaderText="工号" ItemStyle-CssClass="hidden" HeaderStyle-

CssClass="hidden" FooterStyle-CssClass="hidden" />
  <asp:BoundField DataField="姓名" HeaderText="姓名" HeaderStyle-Width="70px"/>
  <asp:BoundField DataField="身份" HeaderText="身份" ItemStyle-CssClass="hidden" HeaderStyle-

CssClass="hidden" FooterStyle-CssClass="hidden" />
  <asp:BoundField DataField="部门" HeaderText="部门" HeaderStyle-Width="100px"/>
  <asp:BoundField DataField="职务" HeaderText="职务" HeaderStyle-Width="100px"/>
  <asp:TemplateField HeaderText="工作业绩(55)" HeaderStyle-Width="100px">
  <ItemTemplate><asp:TextBox id="txtPerformance" runat="server" style="width: 60%"/></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="工作态度(21)" HeaderStyle-Width="100px">
  <ItemTemplate><asp:TextBox id="txtAttitude" runat="server" style="width: 60%"/></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="工作能力(18)" HeaderStyle-Width="100px">
  <ItemTemplate><asp:TextBox id="txtAbility" runat="server" style="width: 60%"/></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="学识水平(6)" HeaderStyle-Width="100px">
  <ItemTemplate><asp:TextBox id="txtKnowledge" runat="server" style="width: 60%"/></ItemTemplate>


  </asp:TemplateField>
  <asp:TemplateField HeaderText="总分" HeaderStyle-Width="100px">
  <ItemTemplate><asp:TextBox id="txtTotal" runat="server" style="width: 60%"/></ItemTemplate>
  </asp:TemplateField>
  </Columns>
  <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
  <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
  <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
  <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
  <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
  <SortedAscendingCellStyle BackColor="#F4F4FD" />
  <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
  <SortedDescendingCellStyle BackColor="#D8D8F0" />
  <SortedDescendingHeaderStyle BackColor="#3E3277" />
  </asp:GridView>
  </td>
  </tr>
  </table>
</asp:Content>

[解决办法]
这个问题不大,在RowDataBind事件中,用 ((TextBox)e.Row.Cells[0].FindControl("txtPerformance")).Text 分别获得前4列的值,转换为整型,然后加起来,最后再传给第五列的TextBox控件
如果还不会,再问哈!
[解决办法]
你可以参考
<%@ Page Language="C#" AutoEventWireup="true" %>

<!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)
{
if (!Page.IsPostBack)
{

System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Intergral", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("ProImage", typeof(System.String)));

System.Random rd = new System.Random(Environment.TickCount); ;

for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dr[4] = i;
dr[5] = "http://dotnet.aspx.cc/Images/meng.gif";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
TotalPrice.Text = totalCount.ToString();
}
}


decimal totalCount = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String n = DataBinder.Eval(e.Row.DataItem, "Num").ToString();
String price = DataBinder.Eval(e.Row.DataItem, "MallPrice").ToString();


int num = 0;
Int32.TryParse(n, out num);
decimal MallPrice = Convert.ToDecimal(price);
totalCount += num * MallPrice;
e.Row.Cells[6].Text = (num * MallPrice).ToString();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("执行SQL DELETE ProductCode=" + GridView1.DataKeys[e.RowIndex].Value.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function jia(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[5].getElementsByTagName("input")[0];
var t = parseInt(num.value, 10);
if (isNaN(t)) num.value = 0;
else num.value = t + 1;
countRow(tr)
}
function jian(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[5].getElementsByTagName("input")[0];
var t = parseInt(num.value, 10);
if (isNaN(t)) num.value = 0;
else {
if (t < 1) return;
num.value = t - 1;
}
countRow(tr)
}
function bian(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
countRow(tr)
}

function countRow(row) {
price = parseFloat(row.cells[3].innerHTML);
if (isNaN(price)) {
row.cells[6].innerHTML = "0"
return;
}
num = row.cells[5].getElementsByTagName("input")[0];
t = parseInt(num.value, 10);
if (isNaN(t)) t = 0;
row.cells[6].innerHTML = roundPrice(price * t);
CountAll();
}
function CountAll() {
var total = 0;
table = document.getElementById('<%=GridView1.ClientID %>');
if (table.rows.length < 3) return;
for (i = 1; i < table.rows.length; i++) {
p = parseFloat(table.rows[i].cells[6].innerHTML);
if (isNaN(p)) p = 0;
total += p;
}
document.getElementById('<%=TotalPrice.ClientID %>').innerHTML = roundPrice(total);
}

function roundPrice(x) {
return Math.round(x * 100) / 100;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tbody>
<tr class="biao"><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
Width="1052px" CssClass="No" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号"></asp:BoundField>
<asp:ImageField DataAlternateTextField="ProImage" DataImageUrlField="ProImage" HeaderText="商品图片">
</asp:ImageField>
<asp:BoundField DataField="ProductName" HeaderText="商品名称" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("MallPrice") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Intergral" HeaderText="赠送积分" />
<asp:TemplateField HeaderText="商品数量">
<ItemTemplate>


<a href="#" onclick="jian(this);return false;">-</a>
<input type="text" id="num" value='<%#Eval("Num")%>' onchange="bian(this)" />
<a href="#" onclick="jia(this);return false;">+</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="小计">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除次商品" ShowDeleteButton="True">
<ControlStyle BorderWidth="0px" Width="40px" />
</asp:CommandField>
</Columns>
</asp:GridView>
</td></tr>
</tbody>
<tfoot>
<tr><td colspan="6">
<asp:Label ID="Label1" runat="server"></asp:Label>
商品总金额: <span>¥<asp:Label ID="TotalPrice" runat="server"></asp:Label></span>元</td>
</tr>
</table>
</form>
</body>
</html>
[解决办法]
function ok(obj) {
var row = obj.parentElement.parentElement;
var txtBox1 = parseInt(row.childNodes[5].childNodes[0].value);
var txtBox2 = parseInt(row.childNodes[6].childNodes[0].value);
var txtBox3 = parseInt(row.childNodes[7].childNodes[0].value);
var txtBox4 = parseInt(row.childNodes[8].childNodes[0].value);
row.childNodes[9].childNodes[0].value = txtBox1 + txtBox2 + txtBox3 + txtBox4;
}
你试试?
[解决办法]
js中用这句:

document.getElementById('<%=TotalPrice.ClientID %>')

就可以获取到<asp:TextBox>里头的数值啊.

热点排行