如何在gridview上将两列相乘并在第三列中显示?

时间:2016-05-24 19:43:28

标签: c# asp.net gridview

如何将2列相乘并在第3列中显示?我想将Price和数量的输入值相乘,并将其显示在lblItemTotal中。我正在考虑添加更新按钮,以便在单击时可以更新所有项目总计。这是我的gridview代码:

<asp:TemplateField HeaderText="Price">
    <ItemTemplate>
        <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
    <ItemTemplate>
        <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Total">
    <ItemTemplate>
        <asp:Label ID="lblItemTotal" runat="server" ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

2 个答案:

答案 0 :(得分:0)

您必须先找到控件才能提取其值。 也许这个链接可以帮助你:
How to find control in TemplateField of GridView?

如果单击更新按钮,则可以遍历gridview中的每一行,并使用链接中描述的方法找到“txtQuantity”和“lblPrice”控件,将它们相乘,然后使用文本设置结果“lblItemTotal”的属性(您也可以使用链接中描述的方法找到它)。

答案 1 :(得分:0)

理想情况下,这种类型的功能应该在客户端上完成(在javascript中)。这是一个完整的工作示例,只需将其复制并粘贴到您的解决方案中即可运行:

代码背后:

public partial class GridTwo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<OrderLineItem> lineItems = new List<OrderLineItem>();
        lineItems.Add(new OrderLineItem { Price = 10.00M, Quantity = 0, ItemTotal = 0.00M });
        lineItems.Add(new OrderLineItem { Price = 100.00M, Quantity = 0, ItemTotal = 0.00M });
        lineItems.Add(new OrderLineItem { Price = 5.00M, Quantity = 0, ItemTotal = 0.00M });

        GridView1.DataSource = lineItems;
        GridView1.DataBind();
    }
}

public class OrderLineItem
{
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public decimal ItemTotal { get; set; }
}

<强> .ASPX:

<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {

            $(".QuantityClass").on('change keyup paste', function () {

                //Ctrl+Shift+J in Google Chrome to bring up the console which allows you to debug javascript
                debugger;

                var textBox = this;
                var quantity = $(textBox).val();
                var tableRows = $(textBox).parent().parent().children();

                if (quantity != "") {
                    var price = tableRows[0].children[0].innerHTML;

                    var itemTotal = price * quantity;

                    tableRows[2].children[0].innerHTML = itemTotal;
                }
                else
                    tableRows[2].children[0].innerHTML = "";
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="txtQuantity" CssClass="QuantityClass" runat="server" Text='<%# Bind("Quantity")%>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Item Total">
                    <ItemTemplate>
                        <asp:Label ID="lblItemTotal" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>

<强>输出: Multiply two columns on a grid view