问题描述
大家好,
我要花很多时间才能找到gridview单元的总数,并在页脚文本框中显示这些值.每次遇到异常时:Microsoft JScript runtime error: Object expected
这是我的gridview代码:
Hi all,
I am struggling a lot to find out the total of gridview cells and displaying the values in a footer textbox. Every time I am getting an exception:Microsoft JScript runtime error: Object expected
Here is my gridview code:
<div>
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
onrowcommand="Gridview1_RowCommand" AutoGenerateColumns="false"
CellSpacing="0" CellPadding="0" Font-Bold="false"
onrowdeleting="Gridview1_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number"/>
<asp:TemplateField HeaderText="Select" ControlStyle-Width="50px" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Width="80px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 1" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Total" Font-Bold="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox2" Width="70px" runat="server" onkeyup="Calculate('Gridview1')"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="total" runat="server" Width="70px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox3" Width="70px" runat="server" ></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CommandName="AddNewRow" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</div>
这是我的JavaScript代码:
Here is my JavaScript code:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
<script type="text/javascript">
function Calculate(GridView)
{
var total = 0;
var gridview = document.getElementById('<%=Gridview1.ClientID %>').getElementsByTagName("input");
for ( i = 0; i < gridview.rows.length; i ++)
{
var node = gridview.rows[i].cells[3].childNodes[3]; //textbox
if (node != undefined && node.type == "text") //check only textbox, ignore empty one
if (!isNaN(node.value) && node.value != "") //check for valid number
total += parseInt(node.value);
}
// document.getElementById("total").innerHTML = total.toString(); //display
var gridview1 = document.getElementById('<%=grdview1.ClientID %>');
gridview1.rows[gridview.rows.length -1].cells[0].innerHTML=total;
}
</script>
我尝试了很多方法,但是每次都遇到相同的异常:Microsoft JScript runtime error: Object expected
我的JavaScript代码似乎有问题.
请有人帮帮我.
I had tried with many ways but every time getting the same exception:Microsoft JScript runtime error: Object expected
It seems something''s wrong with my JavaScript code.
Please somebody help me out.
推荐答案
var gridview1 = document.getElementById(''<%=grdview1.ClientID %>'');
应该是
should be
var gridview1 = document.getElementById(''<%=Gridview1.ClientID %>'');
Gridview1
是GridView控件的ID
Gridview1
is the ID of the GridView control
这篇关于计算javascript中的gridview单元格总数并在页脚中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!