GridView - 使用javascript从Grid Data Row中的控件更改页脚

时间:2014-06-12 11:08:58

标签: c# javascript asp.net gridview

我有一个GridView,如下所示

<asp:GridView ID="grdProducts" runat="server" AutoGenerateColumns="false" OnRowCommand="grdProducts_RowCommand"
                OnRowDataBound="grdProducts_RowDataBound" ShowFooter="true">
                <Columns>
                    <asp:BoundField HeaderText="Product" DataField="ProductName" />
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:HiddenField ID="hfMode" runat="server" />
                            <asp:TextBox ID="txtQty" runat="server" Enabled="false" Text='<%# Eval("Qty") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Unit Price">
                        <ItemTemplate>
                            <asp:Label ID="lblUnitPrice" runat="server" Text='<%# String.Format("{0:#,#.####}",Eval("UnitPrice")) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Total Price">
                        <ItemTemplate>
                            <asp:Label ID="lblTotalPrice" runat="server" Text='<%# String.Format("{0:#,#.####}",Eval("Total")) %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="lblGrandTotal" runat="server"></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

我一直在使用以下代码

更改'lblTotalPrice'值
protected void grdProducts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    TextBox txtQty = (TextBox)e.Row.FindControl("txtQty");
                    Label lblUnitPrice = (Label)e.Row.FindControl("lblUnitPrice");
                    Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
                    txtQty.Attributes.Add("onblur", "CalculateTotal('" + txtQty.ClientID + "','" + lblUnitPrice.ClientID + "','" + lblTotalPrice.ClientID + "')");

                }
            }
            catch (Exception ex)
            {
            }
        }

和javascript

<script type="text/javascript">
        function CalculateTotal(Qty, UnitPrice, Total) {
            document.getElementById(Total).innerHTML = (parseFloat(document.getElementById(Qty).value) * parseFloat(document.getElementById(UnitPrice).innerHTML)).toFixed(2);
        }
    </script>

它的工作没有失败,现在我想知道如何在相同的js函数中更改页脚中lblGrandTotal的文本,但是从行元素中无法访问页脚元素。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

在你的rowDatabound事件中使用类似的内容来查找页脚标签并在javascript中添加其ID

Label lblGrandTotal= (Label)grdProducts.FooterRow.FindControl("lblGrandTotal");