【发布时间】:2013-05-28 04:25:27
【问题描述】:
大家好,目前我正在开发一个 javascript 函数,该函数需要在页脚行中显示 gridview 单元格的总和。现在我能够得到答案,但我无法将值保存在页脚行中。 谁能帮我将值保存到gridview页脚标签。提前致谢。
这是我的js函数。
function addTotal() {
var input = document.getElementsByTagName("input");
var Total = '0.0';
var Sample;
var val;
for (var i = 0; i < input.length; i++) {
if (input[i].type == "text") {
if (input[i].id.indexOf("txtpercent") > 0) {
Sample = document.getElementById(input[i].id).value;
var val = parseFloat(Sample.replace(",", ""));
if (isNaN(val) == true) {
val = 0;
}
Total = parseFloat(Total) + val;
//document.getElementById("Flblallocationpercent").innerHTML=Total;
}
}
}
alert(Total);
if (Total != 100) {
alert("Allocation should be equal to 100 %")
return false;
}
}
这是调用函数的设计部分
<asp:TemplateField HeaderText="Allocation Percentage">
<ItemTemplate>
<asp:TextBox ID="txtpercent" runat="server" Text="" OnTextChanged="allocate_sum"
Visible="true" ToolTip="Percentage" onblur="addTotal()" />
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="Flblallocationpercent" runat="server" Enabled="true" Width="95%" />
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" Width="5%" />
<ItemStyle HorizontalAlign="Center" Width="5%" />
</asp:TemplateField>
【问题讨论】:
-
您是否确保 DOM 具有 ID 为
Flblallocationpercent的元素 -
Flblallocationpercent 是页脚标签的名称。我不确定它是否具有 ID 元素。这就是为什么我评论了那条线
-
默认情况下,服务器端控件的 id 和名称将相同。只需检查该控件是否在 DOM 中,并在取消注释该行后检查该页面中是否有任何 javascript 错误。
-
我收到以下错误。Microsoft JScript 运行时错误:'document.getElementById(...)' is null or not an object
-
所以这意味着该元素不在
DOM中。请检查页面源并确保该元素在 DOM 中。一旦可用,您的代码就会运行良好。
标签: c# .net gridview javascript