?
Solved

Update Price Value in TextBox based on value

Posted on 2008-11-15
6
Medium Priority
?
688 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:nedlogan
6 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22968208
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
 
LVL 1

Accepted Solution

by:
zeesh80 earned 2000 total points
ID: 22968242
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
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22969212
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 26

Expert Comment

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

Author Closing Comment

by:nedlogan
ID: 31517129
Thanks, straightforward and easy to understand.
0
 
LVL 2

Author Comment

by:nedlogan
ID: 22970035
Thanks all for your input. I chose the asp.net solution.

Regards,
NL
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses

621 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