nedlogan
asked on
Update Price Value in TextBox based on value
Hi,
I have 2 text boxes the default value for TextBox1 is "1" this is the amount to be ordered. In the other textbox, TextBox2 the value is the price (taken from the database).
If for example the site visitor changes the value in textbox1 to "3" then the price value in TextBox2 will be multiplied by 3 and displayed.
I'm not sure how best to accomplish this. I have tried "OnTextChanged" but to be honest I'm a bit lost. Code needs to be in C#
The page allows purchase of a single item but the visitor can choose the amount. I don't need anything as complex as a shopping cart.
Any help appreciated.
I have 2 text boxes the default value for TextBox1 is "1" this is the amount to be ordered. In the other textbox, TextBox2 the value is the price (taken from the database).
If for example the site visitor changes the value in textbox1 to "3" then the price value in TextBox2 will be multiplied by 3 and displayed.
I'm not sure how best to accomplish this. I have tried "OnTextChanged" but to be honest I'm a bit lost. Code needs to be in C#
The page allows purchase of a single item but the visitor can choose the amount. I don't need anything as complex as a shopping cart.
Any help appreciated.
Enter Amount: <asp:TextBox ID="TextBox1" Font-Bold="true" Columns="3" runat="server" Visible="true">1</asp:TextBox>
Price: <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("P_ID") %>' />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you use javascript to do that?
Create a JS function on your asp.net page
<script type="text/javascript">
function UpdatePrice(value, txtPrice) {
var old_price = txtPrice.getAttribute("val ue");
alert(value);
alert(old_price);
txtPrice.setAttribute("val ue", old_price * value);
}
</script>
On Page_Load add this:
your_txtQuantity.Attribute s.Add("onb lur", String.Format("UpdatePrice (this.valu e,{0});", YourTxtPrice.ClientID));
Create a JS function on your asp.net page
<script type="text/javascript">
function UpdatePrice(value, txtPrice) {
var old_price = txtPrice.getAttribute("val
alert(value);
alert(old_price);
txtPrice.setAttribute("val
}
</script>
On Page_Load add this:
your_txtQuantity.Attribute
javascript approach as suggested by tiagosalgado looks like the best approach. but thr only problrm will be on browsers where javascript is disabled
ASKER
Thanks, straightforward and easy to understand.
ASKER
Thanks all for your input. I chose the asp.net solution.
Regards,
NL
Regards,
NL
drop another button name Update Cart
and when user click on the update cart button after changing values simply get the new values do the calculation and display the new results