簡體   English   中英

GridView-使用JavaScript從Grid Data Row中的控件更改頁腳

[英]GridView - Change footer from control in Grid Data Row using javascript

我有一個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文本,但是不能訪問行元素中的頁腳元素。 我怎樣才能做到這一點?

在rowDatabound事件中使用類似這樣的內容來查找頁腳標簽,並在javascript中添加其ID

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM