- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- DataKeyNames="ItemID" DataSourceID="sqlDataSource1"
- onrowdatabound="GridView1_RowDataBound">
- <Columns>
- <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
- <asp:BoundField DataField="ItemID" HeaderText="ItemID" InsertVisible="False"
- ReadOnly="True" SortExpression="ItemID" />
- <asp:TemplateField HeaderText="quantity" SortExpression="quantity">
- <EditItemTemplate>
- <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("quantity") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Bind("quantity") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="price" SortExpression="price">
- <EditItemTemplate>
- <asp:TextBox ID="txtPrice" runat="server" Text='<%# Bind("price") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("price") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="amount" SortExpression="amount">
- <EditItemTemplate>
- <asp:TextBox ID="txtAmount" runat="server" Text='<%# Bind("amount") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label3" runat="server" Text='<%# Bind("amount") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
and the RowDataBound event is
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
- TextBox txtPrice = (TextBox)e.Row.FindControl("txtPrice");
- TextBox txtAmount = (TextBox)e.Row.FindControl("txtAmount");
- if (txtQuantity != null)
- {
- txtQuantity.Attributes.Add("onkeyup", "CalcAmount('" + txtQuantity.ClientID + "','" + txtPrice.ClientID + "','" + txtAmount.ClientID + "')");
- txtPrice.Attributes.Add("onkeyup", "CalcAmount('" + txtQuantity.ClientID + "','" + txtPrice.ClientID + "','" + txtAmount.ClientID + "')");
- }
- }
- }
and the javascript method will look like this
- <script type="text/javascript">
- function CalcAmount(quantity, price, amount) {
- document.getElementById(amount).value = parseFloat(document.getElementById(quantity).value) * parseFloat(document.getElementById(price).value);
- }
- </script>
This way when the values are changed in the quantity and price textboxes,
the corresponding amount textbox updates automatically.
No comments:
Post a Comment