Manoj Nayak
Manoj Nayak

Reputation: 2509

How to Update a textbox with a value from another textbox?

I have two textboxes named totalAmount and expenseAmount and another a div called currentAmount in asp.net page(using C#). My requirement is I have to update totalAmount=expenseAmount+currentAmount whenever expenseAmount value changes.I have to achieve this without page refresh and value should be persist even after page refresh.

Upvotes: 0

Views: 1213

Answers (2)

RemS
RemS

Reputation: 280

<script type="text/javascript">
    function xyz() {
        $("#totalAmount")[0].value = parseFloat($("#expenseAmount").val()) + parseFloat($("#currentAmount").val());
    }
</script>
<div>
    <asp:Label ID="Label3" runat="server" Text="currentAmount"></asp:Label>
    <asp:TextBox ID="currentAmount" runat="server" ClientIDMode="Static"></asp:TextBox>
    <br />
</div>
<div>
    <asp:Label ID="Label2" runat="server" Text="expenseAmount"></asp:Label>
    <asp:TextBox ID="expenseAmount" runat="server" onchange="xyz();" ClientIDMode="Static"></asp:TextBox>
    <br />
</div>
<div>
    <asp:Label ID="Label1" runat="server" Text="totalAmount"></asp:Label>
    <asp:TextBox ID="totalAmount" runat="server" ClientIDMode="Static"></asp:TextBox>
</div>

Upvotes: 0

Md. Parves Kawser
Md. Parves Kawser

Reputation: 78

if your code permit to use JQuery, you can do it easily without page refresh.

please write code under document ready.

<script>
    var calculatetotalamount = function () {
        var eamount = parseFloat($("#expenseAmount").val())
        var camount = parseFloat($("#currentAmount").val())
        $("#totalAmount").val(eamount + camount);
    }
    $(function () {

        calculatetotalamount();
        $("#currentAmount").on("change", function () {
            calculatetotalamount();
        });
    });
</script>

Upvotes: 2

Related Questions