Link to home
Start Free TrialLog in
Avatar of nedlogan
nedloganFlag for United Kingdom of Great Britain and Northern Ireland

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.  
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") %>' />

Open in new window

Avatar of Faizan Sarwar
Faizan Sarwar
Flag of United Kingdom of Great Britain and Northern Ireland image

In its simple form
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
ASKER CERTIFIED SOLUTION
Avatar of zeesh80
zeesh80

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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("value");
            alert(value);
            alert(old_price);
            txtPrice.setAttribute("value", old_price * value);
        }
       
    </script>

On Page_Load add this:

your_txtQuantity.Attributes.Add("onblur", String.Format("UpdatePrice(this.value,{0});", YourTxtPrice.ClientID));

javascript approach as suggested by tiagosalgado looks like the best approach. but thr only problrm will be on browsers where javascript is disabled
Avatar of nedlogan

ASKER

Thanks, straightforward and easy to understand.
Thanks all for your input. I chose the asp.net solution.

Regards,
NL