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
Solved

call function

Posted on 2016-10-05
8
54 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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