Solved

OnBlur / OnFocus Events in ASP.NET 1.1

Posted on 2009-05-10
9
822 Views
Last Modified: 2013-11-07
I am developing in ASP.NET 1.1 VB 2003. I have three text boxes Value1, Value2 and Value3. Value3 is supposed to hold the sum of Value1 and Value2. I wish if a value is keyed in Value1 and Value2, then when either Value2 loses focus or Value3 gets the focus, then sum should be automatically calculated in Value3.

I have tried to use either OnBlur or OnFocus and failed to get a solution. Please assist.

Nduguyettu
0
Comment
Question by:nduguyettu
  • 4
  • 4
9 Comments
 
LVL 15

Expert Comment

by:OMC2000
ID: 24349094
As far as I remember you could assign client side functions for these events.

It should be like

<... onblur="calculateSum(this)" />

where calculateSum() is client side javascript function, which may accept object reference in order to give user more information if necessary. Practically it would be enough to get values from two elements and assign their sum to the third one
0
 

Author Comment

by:nduguyettu
ID: 24351493
May you give me a complete function,  and where to place it.

Thanks,

Nduguyettu
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 24351708
somthing like the following:

<script language="javascript">
function calculateSum()
{
    var f = document.form1;
    v1 = f.Value1.value;
    v2 = f.Value2.value;
    if( v1.length == 0 || v2.length == 0 ) {
        f.Value3.value = "";
        return;
    }
    try {
       f1 = parseFloat(v1);
       f2 = parseFloat(v2);
       f3 = f1 + f2;
       f.dsc.value = f3;
    } catch (e) {
      alert('bad value ' + e.description);
      return false;
    }
}
</script>

<form id="form1" runat="server">
<asp:TextBox ID="Value1" value="12" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value2" value="4" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value3" runat="server" onfocus="calculateSum()"/>
</form>
0
 

Author Comment

by:nduguyettu
ID: 24351960
Hello OMC2000,

Thanks for the above code. I am sure this will help me but am facing an JScript runtime error which am failing to understand. It is at line

v1 = f.Value1.value;

It says "'Value1' is null or not an object"

May you give more help.

Thanks,

Nduguyettu
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:OMC2000
ID: 24355310
it assumes that form is named f1 and it has controls Value1, Value2 and Value3, as you stated in your original question. The following version works with the first form on the page

<script language="javascript">
function calculateSum()
{
    var f = document.forms[0];
    v1 = f.Value1.value;
    v2 = f.Value2.value;
    if( v1.length == 0 || v2.length == 0 ) {
        f.Value3.value = "";
        return;
    }
    try {
       f1 = parseFloat(v1);
       f2 = parseFloat(v2);
       f3 = f1 + f2;
       f.dsc.value = f3;
    } catch (e) {
      alert('bad value ' + e.description);
      return false;
    }
}
</script>

<form id="form1" runat="server">
<asp:TextBox ID="Value1" value="12" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value2" value="4" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value3" runat="server" onfocus="calculateSum()"/>
</form>
0
 

Author Comment

by:nduguyettu
ID: 24356314
Thanks OMC2000, but another issue has come up which I have failed to understand. May you explain what dsc means in the line below

f.dsc.value = f3;

It is returning an error "bad value 'dsc' is null or not an object"

Thanks,

Nduguyettu
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 500 total points
ID: 24356542
I'm sorry, I tested it upon old sample and than renamed variables, I missed that one

<script language="javascript">
function calculateSum()
{
    var f = document.forms[0];
    v1 = f.Value1.value;
    v2 = f.Value2.value;
    if( v1.length == 0 || v2.length == 0 ) {
        f.Value3.value = "";
        return;
    }
    try {
       f1 = parseFloat(v1);
       f2 = parseFloat(v2);
       f3 = f1 + f2;
       f.Value3.value = f3;
    } catch (e) {
      alert('bad value ' + e.description);
      return false;
    }
}
</script>

<form id="form1" runat="server">
<asp:TextBox ID="Value1" value="12" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value2" value="4" runat="server" onblur="calculateSum()"/>
<asp:TextBox ID="Value3" runat="server" onfocus="calculateSum()"/>
</form>
0
 
LVL 17

Expert Comment

by:ToAoM
ID: 24359714
Also make sure you reference the ClientID's of the textboxes. If the textbox is in a usercontrol or table, or wizard control, it will render a different ID than the actual serverside ID.
0
 

Author Closing Comment

by:nduguyettu
ID: 31579914
I wish to thank the expert for the professional way he answered the question, the patience he showed while answering all the counter queries. I wish also to thank the expert for giving a full example that enabled me to solve my problem. I wish all experts would give full solutions other than portions.

Nduguyettu
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

9 Experts available now in Live!

Get 1:1 Help Now