Link to home
Start Free TrialLog in
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
Avatar of Russ Suter
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.
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;
Avatar of 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

Avatar of 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

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?
Avatar of 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
SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial