I'm new to ASP.NET. I have a situation like below:

<asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server">
            <asp:BoundField DataField="BookID" HeaderText="Book ID" />
            <asp:BoundField DataField="BookName" HeaderText="Book Name" />
            <asp:BoundField DataField="Author" HeaderText="Author" />
            <asp:BoundField DataField="Price" HeaderText="Price" />
            <asp:TemplateField HeaderText="Testing Field">
                    <asp:HiddenField ID="HiddenPrice" Value='<%#Eval("Price") %>' runat="server" />
                    <asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged" ID="ddlQuantity" runat="server">
                    <asp:TextBox ID="Total" Enabled="false" Text="asdfsdf" runat="server"></asp:TextBox>

As you can see I have a dropdownlist inside a gridview. Every time I choose a different number in the list, it calculates the price and add to the Total Textbox (Price*Number).

So my question is how can I calculate the total price of all Total textbox and output the result to the label outside girdview?

Here is my code behind file:

private static OBMDbContext context = new OBMDbContext();
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            GridView1.DataSource = context.Books.ToList();

    protected void ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
        DropDownList ddl = (DropDownList)sender;
        int quantity = Convert.ToInt16(ddl.SelectedItem.Value);
        TextBox tb = (TextBox)ddl.FindControl("Total");
        HiddenField hf = (HiddenField)ddl.FindControl("HiddenPrice");
        tb.Text = (quantity * Convert.ToInt16(hf.Value)).ToString();

Thanks in advance :)

Related posts

Recent Viewed