Solved

OnBlur / OnFocus Events in ASP.NET 1.1

Posted on 2009-05-10
9
832 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 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: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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 57
ajax jquery 3 25
jquery tab header text 1 22
Change to file doesn't show up 16 50
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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