如何在ASP.NET C#上获取和显示Gridview中的特定值

时间:2017-09-17 07:29:12

标签: c# asp.net gridview

我目前正在开发一个在线购物车项目。

我希望程序在GridView外的标签中显示GetTotal()

<asp:Content ID="Content1" ContentPlaceHolderID="C1" Runat="Server">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="832px"    >
        <Columns>
            <asp:TemplateField HeaderText="Product Image">
                <ItemTemplate>
                    <asp:Image ImageUrl='<%# "../" + Eval("Product_Image") %>' runat="server"  Height="100px" Width="100px"/>

                      </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product Series">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Product_Series") %>'></asp:Label>
                      </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product Name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("Product_Name") %>'></asp:Label>
                      </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product Price">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%#Eval("Product_Price") %>'></asp:Label>
                      </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Product Quantity">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:Label>
                      </ItemTemplate>
                 <EditItemTemplate>
                     <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:TextBox>
                     </EditItemTemplate>
                  <FooterTemplate>
                    Total Amount:
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total Price">
                <ItemTemplate>
                    <%# GetUnitPrice(Decimal.Parse(Eval("Product_Price").ToString())*Decimal.Parse(Eval("Product_Quantity").ToString())).ToString("N2")   %>
                      </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="l6" runat="server" Text=' <%# GetTotal().ToString("N2") %>'></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>

            <asp:CommandField ShowEditButton="true"/>
            <asp:CommandField ShowDeleteButton="true"/>

             </Columns>

    </asp:GridView>

GridView中的所有值都是通过另一个页面的会话传递的,不包括PriceTotalUnitPrice,其中计算在下面的脚本中执行:

<script runat="server">

decimal TotalUnitPrice;
decimal GetUnitPrice(decimal Price)
{
    TotalUnitPrice += Price;
    return Price;
}
decimal GetTotal()
{
    return TotalUnitPrice;
}

</script>

关于如何做到这一点的任何想法?原因是我希望程序通过标签获取GetTotal()的值,并根据它向用户收费。

1 个答案:

答案 0 :(得分:0)

您在GridView页脚标签GetTotal()中绑定16,以便您可以从那里获取并设置为标签外:

// check if GridView is not empty
if (GridView1.Rows.Count != 0)
{
    string total = ((Label)GridView1.FooterRow.FindControl("16")).Text;
    LabelOutside.Text = total;
}