Posted on 2009-04-15
Last Modified: 2012-05-06
Following are the two bound textboxes in the gridview:

First Textbox:
<asp:TextBox ID="TextBox12" runat="server" Text='<%# Eval("PAYMENT_AMOUNT") %>'></asp:TextBox>

Second Textbox:

 <asp:TextBox ID="txt11" runat="server" Text='<%# Eval("PRINCIPAL_AMOUNT") >'></asp:TextBox>

Not bound Textbox:

                    <asp:TextBox ID="txt6" runat="server" Text=></asp:TextBox>

The First Question is how can i take the difference of first two text boxes in the third textbox (i.e;  PRINCIPAL_AMOUNT-     PAYMENT_AMOUNT).
Should i write like this :    

                    <asp:TextBox ID="txt6" runat="server" Text=></asp:TextBox = PRINCIPAL_AMOUNT-     PAYMENT_AMOUNT>

Second question is how can i make the client side event of textbox leave? What i want is when the cursor leaves the first or second textbox, third textbox should automatically be updated with the above formula.

Please suggest.              
Question by:salmanfazal
    LVL 3

    Expert Comment

    For question 1, you could do something like <%# Eval("PRINCIPAL_AMOUNT") - Eval("PAYMENT_AMOUNT") > as long as they're both numeric types, or Convert.ToDecimal(Eval("PRINCIPAL_AMOUNT")) - Convert.ToDecimal(Eval("PAYMENT_AMOUNT")) if they're not.  You could also create a function in your codebehind like protected string GetBalance() that returns the difference, formatted however you want, then just use <%# GetBalance() %> in your textbox declaration.

    For question 2, you'll need to first write a javascript function to do the calculation, then in the Page_Load event, use TextBox12.Attributes.Add("onchange", "calculateBalance();");  assuming you called the javascript function calculateBalance.  That will add an onchange event handler to the output control, which will fire when the value of the textbox has changed and the focus leaves the control.  It will not fire if focus leaves the control with the same value.

    Accepted Solution

    i tried the first question and the error comes like this: Operator '-' is not defined for type 'DBNull' and type 'DBNull'. in both the fields there is no null values. if its empty the value is 0. what could be the reason behind this.
    below is my code part getting error.

    <asp:TemplateField HeaderText="Inst. Balance">
                        <asp:TextBox ID="TextBox9" runat="server" text ='<%#Eval("PRINCIPAL_AMOUNT")-Eval("PAYMENT_AMOUNT")%>' ></asp:TextBox>
                        <asp:Label ID="Label21" runat="server" text ='<%#Eval("PRINCIPAL_AMOUNT")-Eval("PAYMENT_AMOUNT")%>' ></asp:Label>

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    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.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    730 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

    15 Experts available now in Live!

    Get 1:1 Help Now