Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

OnBlur / OnFocus Events in ASP.NET 1.1

Posted on 2009-05-10
9
Medium Priority
?
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 2000 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:Jesse Houwing
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

650 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