简体   繁体   中英

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.

<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:

<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.

Your GetTotal() in binding in GridView footer Label 16 so you can get it from there and set to outside Label:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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