Solved

OnBlur / OnFocus Events in ASP.NET 1.1

Posted on 2009-05-10
9
831 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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