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

LVL 2
nedloganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sm394Commented:
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
0
zeesh80Commented:
Hello,
Please see the code below, i am not taking any value from the database but hardcoding it with any value, hope you get an idea from this.
/***************************************************************/
 <asp:TextBox ID="txtOne" runat="server" Text="1" AutoPostBack="True" 
            ontextchanged="txtOne_TextChanged"></asp:TextBox>
    
       <asp:TextBox ID="txtTwo" runat="server" Text="2"></asp:TextBox>
   
/*******************************************************************/
 protected void txtOne_TextChanged(object sender, EventArgs e)
    {
        int i = 0;
        int j = 0;
        i = Convert.ToInt32(txtOne.Text);
        j = i * 3;
        txtTwo.Text = Convert.ToString(j);
 
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tiagosalgadoCommented:
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));

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Anurag ThakurTechnical ManagerCommented:
javascript approach as suggested by tiagosalgado looks like the best approach. but thr only problrm will be on browsers where javascript is disabled
0
nedloganAuthor Commented:
Thanks, straightforward and easy to understand.
0
nedloganAuthor Commented:
Thanks all for your input. I chose the asp.net solution.

Regards,
NL
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.