dynamically calculate the price in Gridview when the quantity goes up

I have the following gridview . I want the price to be calculated automatically as soon as the user increases or decreases the quantity.

dynamically calculate the price in Gridview when the quantity goes up

Below is my gridview code:

<asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" GridLines="None" OnRowUpdating="test">
                <Columns>
                    <asp:BoundField DataField="item" HeaderText="Item" HeaderStyle-Font-Bold="true" ItemStyle-Width="35%"  />
                    <asp:TemplateField HeaderText="Description" HeaderStyle-Font-Bold="true" ItemStyle-Width="35%"  >
                        <ItemTemplate>
                            <asp:Label ID="lblDesc" runat="server" Text='<%# Eval("child") %>' /><br />
                            &nbsp;Requested By: &nbsp;<asp:Label ID="lblDesc1" runat="server" Text='<%# Eval("RequestedBy") %>' />
                        </ItemTemplate>

                    </asp:TemplateField>
                       <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="10%" >
                        <ItemTemplate>
                                <input type="number" id="txtQuantity" value='<%# Eval("Quantity") %>'   style="text-align:right;" data-mini="true" min="1" max="100" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                       <asp:TemplateField HeaderText="Price" ItemStyle-Width="100%" HeaderStyle-HorizontalAlign="Right">
                        <ItemTemplate>
                                <asp:Label  ID="lblPrice" runat="server" Text= '<%# Int32.Parse(Eval("Quantity").ToString()) * 28 %>' />
                        </ItemTemplate>
                      </asp:TemplateField>

                </Columns>

            </asp:GridView>

Is it also possible to capture the new calculated price and quantity inside the code behind.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

**ASPX**
 <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="10%" >
                        <ItemTemplate>
                            <asp:TextBox ID="txtqty" runat ="server" TextMode="Number" MaxLength="100" Text='<%# Eval("Quantity") %>' OnTextChanged ="txtqty_TextChanged" AutoPostBack ="true"></asp:TextBox>
                                
                        </ItemTemplate>
                    </asp:TemplateField>

CODE BEHIND
Protected Sub txtqty_TextChanged(sender As Object, e As EventArgs)
        Dim textBox As TextBox = CType(sender, TextBox)

        
        Dim qty As String = textBox.Text
        Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
        Dim lblPrice As Label = CType(row.FindControl("lblPrice"), Label)

        lblPrice.Text = Convert.ToInt32(qty) * 28
    End Sub


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x