简体   繁体   English

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

[英]How to obtain and display specific value in Gridview on ASP.NET C#

I am currently developing an online shopping cart project. 我目前正在开发一个在线购物车项目。

I would like the program to display the GetTotal() in a label outside of the 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>

All values in the GridView are passed on via session from another page, excluding Price and TotalUnitPrice , where calculations are carried out in the script below: GridView中的所有值都是通过会话从另一个页面传递的,不包括PriceTotalUnitPrice ,在下面的脚本中进行计算:

<script runat="server">

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

</script>

Any ideas on how to do this? 有关如何执行此操作的任何想法? The reason is that I would like the program to obtain the value of GetTotal() via the label and charge the user based on it. 原因是我希望程序通过标签获取GetTotal()的值,并根据该值向用户收费。

Your GetTotal() in binding in GridView footer Label 16 so you can get it from there and set to outside Label: 您在GridView页脚Label 16中绑定的GetTotal() ,因此您可以从那里获取它并设置为外部Label:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM