?
Solved

call function

Posted on 2016-10-05
8
Medium Priority
?
68 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 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