Avatar of mcrmg
mcrmg
 asked on

call function

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
ASP.NET

Avatar of undefined
Last Comment
Karen

8/22/2022 - Mon
Russ Suter

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.
Karen

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;
mcrmg

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mcrmg

ASKER
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

Karen

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?
mcrmg

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Karen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.