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.              
Who is Participating?
salmanfazalConnect With a Mentor Author Commented:
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>
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.
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.

All Courses

From novice to tech pro — start learning today.