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

<asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server">
        <Columns>
            <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">
                <ItemTemplate>
                    <asp:HiddenField ID="HiddenPrice" Value='<%#Eval("Price") %>' runat="server" />
                    <asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged" ID="ddlQuantity" runat="server">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                        <asp:ListItem>8</asp:ListItem>
                        <asp:ListItem>9</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="Total" Enabled="false" Text="asdfsdf" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

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();
            GridView1.DataBind();
        }
    }

    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