【问题标题】:How to obtain and display specific value in Gridview on ASP.NET C#如何在 ASP.NET C# 上的 Gridview 中获取和显示特定值
【发布时间】:2018-02-25 22:51:03
【问题描述】:

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

我希望程序在 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()的值,并据此向用户收费。

【问题讨论】:

  • GridView 会有很多行,对吧?因此,每个产品都有TotalUnitPrice。在这种情况下,您可以使用&lt;asp:CommandField /&gt;,当您选择特定产品时,其TotalUnitPrice 将显示在下面的标签中。
  • 但是我需要程序根据上面显示的 TotalUnitPrice 向用户收费。也许使用标签不是最好的方法?
  • TotalUnitPrice 值已经在GridView 中,因此您可以随时在后端代码中访问它。所以我猜你不需要单独的标签来显示价格。要在标签中显示它,无论如何您都必须从 GridView 访问TotalUnitPrice。相反,您可以从 GridView 访问它,然后直接将其发送给用户。

标签: c# asp.net gridview


【解决方案1】:

您的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;
}

【讨论】:

  • 谢谢,它运行良好,但是如果我在GridView 为空时访问此页面,则会引发错误Object reference not set to an instance of an object.。知道如何解决这个问题吗?
  • 您可以简单地检查gridview是否为空:if(GridView1.Rows.Count != 0)
猜你喜欢
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
相关资源
最近更新 更多