Update Price Value in TextBox based on value

Posted on 2008-11-15
Last Modified: 2012-05-05
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

Question by:nedlogan
    LVL 13

    Expert Comment

    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
    LVL 1

    Accepted Solution

    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" 
           <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

    LVL 8

    Expert Comment

    Why don't you use javascript to do that?

    Create a JS function on your page

    <script type="text/javascript">
            function UpdatePrice(value, txtPrice) {

                var old_price = txtPrice.getAttribute("value");
                txtPrice.setAttribute("value", old_price * value);

    On Page_Load add this:

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

    LVL 26

    Expert Comment

    by:Anurag Thakur
    javascript approach as suggested by tiagosalgado looks like the best approach. but thr only problrm will be on browsers where javascript is disabled
    LVL 2

    Author Closing Comment

    Thanks, straightforward and easy to understand.
    LVL 2

    Author Comment

    Thanks all for your input. I chose the solution.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now