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

时间:2022-01-01 21:02:00

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中的所有值都是通过另一个页面的会话传递的,不包括Price和TotalUnitPrice,其中计算在下面的脚本中执行:

<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()的值,并根据它向用户收费。

1 个解决方案

#1


0  

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

你的GetTotal()绑定在GridView页脚标签16中,这样你就可以从那里得到它并设置为标签外:

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

#1


0  

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

你的GetTotal()绑定在GridView页脚标签16中,这样你就可以从那里得到它并设置为标签外:

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