Solved

call function

Posted on 2016-10-05
8
58 Views
Last Modified: 2016-10-12
Hi,

this is my code
 <asp:TextBox id="SqFtX" runat="server" Width="40px" onkeypress="return isNumber(event,$('#SqFtX').val())"></asp:TextBox>

Open in new window


I am checking to see if user entered numeric value. Is there a way to call another function at the same time? such as

 <asp:TextBox id="SqFtX" runat="server" Width="40px" onkeypress="return isNumber(event,$('#SqFtX').val()); MyFunction();"></asp:TextBox>

Open in new window


Thanks
0
Comment
Question by:mcrmg
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41830854
Basically, no. What you're actually calling is an event handler. An event can have only one event handler assigned to it. It would be possible to call a function from within the event handler. It's also possible to switch on some variable within the event handler so you can optionally run additional functions.
0
 
LVL 9

Expert Comment

by:Karen
ID: 41830942
It is the "return" statement that is stopping the second function call.
Depending on what you want, any of the following should work

isNumber(event,$('#SqFtX').val()); MyFunction(); return;

var retval = isNumber(event,$('#SqFtX').val()); MyFunction(); return retval;

var retval = isNumber(event,$('#SqFtX').val()); if (retval) { MyFunction(); } return retval;
0
 

Author Comment

by:mcrmg
ID: 41831624
I think this should work

var retval = isNumber(event,$('#SqFtX').val()); if (retval) { MyFunction(); } return retval;

When the value is numeric, it will call MyFunction.  But for some reason, it is not.  Am I missing something?  thanks

<td><asp:TextBox id="SqFt" runat="server" Width="80px" onkeypress="var retval = isNumber(event,$('#SqFtX').val()); if (retval) { MyFunction(); } return retval;"></asp:TextBox>

Open in new window


    protected void MyFunction(object sender, EventArgs e)
    {
        System.Windows.Forms.MessageBox.Show("HERE");


    }

Open in new window

0
Technology Partners: 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:mcrmg
ID: 41831898
Is there a way to call to a code behind function?  This is my code, it looks like it posts back, but not getting to the function.  I would like to see if there is a way to verify if the entry is numeric then call to a function.  thanks

<asp:TextBox id="SqFt" runat="server" Width="80px" AutoPostBack="True" onkeypress="return isNumber(event,$('#SqFt').val())" Onclick ="MyFunction"></asp:TextBox>

Open in new window

0
 
LVL 9

Expert Comment

by:Karen
ID: 41832824
It is different depending on whether MyFunction is in the javascript or in your code-behind. Sorry, I thought it was a javascript function you wanted to call.
When do you want the code-behind to execute? While the user is still typing in the text box (i.e. triggered by keypress event) or after they have finished typing?
0
 

Author Comment

by:mcrmg
ID: 41833590
if it is possible, I would like to "block" user from entering non-numeric value (during or after validation is okay), then once they leave the field, a code behind function will be called.  That function is basic grabbing the value that user just entered and do some calculations.  thanks
0
 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 250 total points
ID: 41834330
This is the sort of thing that Javascript is excellent at. You can have click validation on the client side which can short circuit postback events if the result is not as desired. Something like this:
<asp:TextBox Id="myTextBox" onkeypress="validateKeyPress();" onclick="myTextBox_Click" onclientclick="return isInputValid(this, event);"></asp:TextBox>

Open in new window

What happens is the onclientclick event (client-side click event) is handled first. Somewhere you have a Javascript function that looks like this:
function isInputValid(sender, eventArgs) { ... }

Open in new window

That function returns a true or false value. If the returned value is false then the server-side click event is prevented.

DISCLAIMER: The above was just spit out of my brain without any checking but essentially that's the correct idea.
0
 
LVL 9

Accepted Solution

by:
Karen earned 250 total points
ID: 41836267
You just have to split your two tasks up.

Your code to stop the user from entering non-numeric characters is done in onkeypress.
Then, the same code is executed in onchange. If it fails the test, then it returns "false" and OnTextChanged is not executed. If it passes the test, then OnTextChanged is executed.

<asp:TextBox id="SqFt" runat="server" Width="80px" AutoPostBack="True"  onkeypress="return isNumber(event,$('#SqFt').val())" onchange="if (!isNumber(event,$('#SqFt').val())) return false;" OnTextChanged="MyFunction"></asp:TextBox>
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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