Solved

call function

Posted on 2016-10-05
8
40 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now