locked
dynamically calculate the price in Gridview when the quantity goes up RRS feed

  • Question

  • User351619809 posted

    Hello All,

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

    enter image description here

    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.

    Monday, December 7, 2020 5:30 AM

Answers

  • User1535942433 posted

    Hi anjaliagarwal5@yahoo.com,

    Accroding to your description,as far as I think,you could use textbox with TextMode="Number" to replace the input control.It will be render as input.

    And you could use textchanged event.When you change the Quantity,it will fire the textchanged event. And the you could set the price.

    Just like this:

    <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" GridLines="None" OnRowDataBound="grdShoppingCart_RowDataBound" >
    				<Columns>
    					 <asp:TemplateField HeaderText="Id" HeaderStyle-HorizontalAlign="Right" ItemStyle-Width="35%">
                            <ItemTemplate>
    								<asp:Label  ID="lbId" runat="server" Text='<%#Eval("Id") %>'  />
                            </ItemTemplate>
                          </asp:TemplateField>  
    					   <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="10%" >
                            <ItemTemplate>
                                <asp:TextBox ID="txtQuantity" runat="server" TextMode="Number" OnTextChanged="Server_Changed" AutoPostBack="true" Text='<%#Eval("num") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
    					   <asp:TemplateField HeaderText="Price" ItemStyle-Width="100%" HeaderStyle-HorizontalAlign="Right">
                            <ItemTemplate>
    								<asp:Label  ID="lblPrice" runat="server"  />
                            </ItemTemplate>
                          </asp:TemplateField>
    				</Columns>
    			</asp:GridView>

    Code-behind:

      protected void Server_Changed(object sender, EventArgs e)
            {
                TextBox changeTextbox = sender as TextBox;
                GridViewRow row = (GridViewRow)changeTextbox.NamingContainer;
                TextBox tbq = (row.FindControl("txtQuantity") as TextBox);
                Label lbl = (row.FindControl("lblPrice") as Label);
                lbl.Text = (Convert.ToInt32(tbq.Text) * 28).ToString();
            }
            protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lbl = (e.Row.FindControl("lblPrice") as Label);
                    TextBox tbq = (e.Row.FindControl("txtQuantity") as TextBox);
                    lbl.Text = (Convert.ToInt32(tbq.Text) * 28).ToString();
                }
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2020 8:57 AM

All replies

  • User351619809 posted

    any help or hint on this will be highly appreciated.

    Monday, December 7, 2020 11:34 AM
  • User1535942433 posted

    Hi anjaliagarwal5@yahoo.com,

    Accroding to your description,as far as I think,you could use textbox with TextMode="Number" to replace the input control.It will be render as input.

    And you could use textchanged event.When you change the Quantity,it will fire the textchanged event. And the you could set the price.

    Just like this:

    <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" GridLines="None" OnRowDataBound="grdShoppingCart_RowDataBound" >
    				<Columns>
    					 <asp:TemplateField HeaderText="Id" HeaderStyle-HorizontalAlign="Right" ItemStyle-Width="35%">
                            <ItemTemplate>
    								<asp:Label  ID="lbId" runat="server" Text='<%#Eval("Id") %>'  />
                            </ItemTemplate>
                          </asp:TemplateField>  
    					   <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="10%" >
                            <ItemTemplate>
                                <asp:TextBox ID="txtQuantity" runat="server" TextMode="Number" OnTextChanged="Server_Changed" AutoPostBack="true" Text='<%#Eval("num") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
    					   <asp:TemplateField HeaderText="Price" ItemStyle-Width="100%" HeaderStyle-HorizontalAlign="Right">
                            <ItemTemplate>
    								<asp:Label  ID="lblPrice" runat="server"  />
                            </ItemTemplate>
                          </asp:TemplateField>
    				</Columns>
    			</asp:GridView>

    Code-behind:

      protected void Server_Changed(object sender, EventArgs e)
            {
                TextBox changeTextbox = sender as TextBox;
                GridViewRow row = (GridViewRow)changeTextbox.NamingContainer;
                TextBox tbq = (row.FindControl("txtQuantity") as TextBox);
                Label lbl = (row.FindControl("lblPrice") as Label);
                lbl.Text = (Convert.ToInt32(tbq.Text) * 28).ToString();
            }
            protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lbl = (e.Row.FindControl("lblPrice") as Label);
                    TextBox tbq = (e.Row.FindControl("txtQuantity") as TextBox);
                    lbl.Text = (Convert.ToInt32(tbq.Text) * 28).ToString();
                }
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2020 8:57 AM
  • User351619809 posted

    Thank You!!

    Tuesday, December 8, 2020 6:42 PM