嘿伙计们,我正在尝试添加在动态 gridview 中输入的值,并使用 javascript 在 gridview 外部的文本框中显示总和。下面是我尝试过的代码...当我输入任何值时,我只得到 0显示在文本框中。我希望总和显示在文本框中。你们能告诉我哪里出错了吗?
我的 JavaScript 代码
<script type="text/javascript">
function showCal(obj){
var table = document.getElementById("Gridview1");
var sum = 0;
for (var i = 1; i < table.rows.length; i++)
{
if (isNaN(table.rows[i].cells[1].innerText != null))
{
if (!isNaN(table.rows[i].cells[1].innerText))
{
sum = (parseFloat(sum) + parseFloat(table.rows[i].cells[1].innerText)).toString();
}
}
document.getElementById("TextBox4").innerText = sum;
}
}
我的 ASP.NET 代码
PagerStyle-Mode="NumericPages"
AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated" Height="16px">
<PagerStyle CssClass="pgr" Height="25px" BorderStyle="Solid" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />
<asp:TemplateField HeaderText="Air/Bus/Train Fare">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" CssClass="txtBoxNormalmedium" onkeyup="showCal(this);" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City To">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" CssClass="txtBoxNormalmedium" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City From">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" CssClass="btnNormalAdd" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" CssClass="lnkbut">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:TextBox ID="TextBox4" runat="server" onkeyup="showCal(this);" ></asp:TextBox>