Call javascript function from onTextChangedEvent

Last post 08-18-2009 12:49 PM by NC01. 2 replies.

Sort Posts:

  • Call javascript function from onTextChangedEvent

    08-18-2009, 11:05 AM
    • Member
      2 point Member
    • geekgirl1978
    • Member since 07-29-2009, 2:56 PM
    • Posts 14

     Hi everyone. Hope someone can help.

    I have a page with a asp.net textbox on it where the user can specify a quantity for an item they are ordering. I also have a asp.net label which displays the subtotal. When the user changes the value in the quantity textbox I want the subtotal to update. I know how to do the calculation etc but I can't figure out how to call a function to do the calculation as soon as the value in the textbox changes.

    It works if i use the onTextChanged event and click off the textbox when the quantity has been changed but I want it to happen when the contents on the textbox change.

    I have searched the forums for help and have managed to figure out that I need to use javascript but am not sure how to do it. I think I need to call the javascript function using the onchange event but then how do I update the subtotal label?

  • Re: Call javascript function from onTextChangedEvent

    08-18-2009, 11:53 AM

    I think the best you can do is handle the onblur event.  This will trigger when the focus is lost from the textbox.

    Edit: You can also use the onchange event, which will only trigger if the text is actually changed, otherwise it doesn't trigger.

  • Re: Call javascript function from onTextChangedEvent

    08-18-2009, 12:49 PM
    Answer
    • All-Star
      75,105 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,965
    • TrustedFriends-MVPs

    Try this:

    <asp:TextBox ID="TextBox1" runat="server" onchange="textBox1OnChange(this);"></asp:TextBox>

    <!-- Use a TextBox that looks like a Label since otherwise, if the page does a PostBack the values will be lost -->
    <asp:TextBox id="TextBox1Label" BorderColor="White" BorderStyle="None" BorderWidth="0" runat="server"></asp:TextBox>

    <script type="text/javascript">
    <!--
    function buttonOnClick(elementRef)
    {
     var labelId = elementRef.id + 'Label';

     // Do the calculations here...
     var calculatedValue = elementRef.value;

     document.getElementById(labelId).value = calculatedValue;
    }

    // Need to set the readOnly property here, since setting it in the markup does not persist client-side changes.
    function windowOnLoad()
    {
     document.getElementById('<%= TextBox1Label.ClientID %>').readOnly = true;
    }

    window.onload = windowOnLoad;
    // -->
    </script>

    NC...

Page 1 of 1 (3 items)