Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Re: How to 'set focus' on an input form

$
0
0
Re: How to 'set focus' on an input form
Web Connection 5.0
Re: How to 'set focus' on an input form
Dec. 9, 2012
01:22 pm
3NW0SNP9WShow this entire thread in new window
Gratar Image based on email address
From:Rick Strahl
To:BrianW

Things like Validation are typically better to do on the client side - either using pure JavaScript or by making an AJAX call to validate/correct input.

The problem with what you're doing is that you're posting back to the server, so the entire form is essentially sent to the server and then re-rendered. When you come back the focus reverts back to its default control.

The better approach is the capture the change event of the text control and perform your actions there. There's no Proper() function in JavaScript. Something like this:

<divclass="containercontent"> <ww:wwWebTextBoxrunat="server"id="txtName" /><ww:wwWebButtonrunat="server"ID="btnSave"Text="Save" /></div><%= this.Page.JqueryConfig.IncludejQuery("CDN",.T.) %><scripttype="text/javascript"> $().ready(function () { $("#txtName").change(function () { var $name = $(this) var text = $name.val(); $name.val(toTitleCase(text)); }); }); function toTitleCase(str) { return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } </script>

If you want to call back with an AJAX callback (ie. validate against hte server) you can use the wwAjaxCallback control to make that easily happen. Define a method on the Page and then use the control to call a method.

Here's the FoxPro method in the Page:

FUNCTION ToProperCase(lcText)RETURNPROPER(lcText)ENDFUNC

Then in the HTML page replace the code that includes jQuery with an wwAjaxMethodCallback control and change the script code:

<ww:wwAjaxMethodCallbackrunat="server"id="Proxy" /><scripttype="text/javascript"> $().ready(function () { $("#txtName").change(function () { var $name = $(this) var text = $name.val(); // local JavaScript call //$name.val(toTitleCase(text)); // Server call Proxy.callMethod("ToProperCase",[text],function (result) { $name.val(result); }); }); });</script>

It's super simple to make AJAX callbacks in Web Control pages.


+++ Rick ---


Have an entry form - textboxes for name, Address etc and I want to clean up the input before the record is written by applying a Proper() command. As example for the Christian Name (txtChrName) box I apply 'Change="txtChrNameToProper" AutoPostBack="True" ' and have a Function

FUNCTION txtChrNameToProper()
this.txtChrName.text = PROPER(this.txtChrName.Text)
ENDFUNC

Great and works fine so that when the user tabs out of the chrname field it corrects any bad input BUT it then sets the focus somewhere and the user has to use the mouse tofocus on the next field for input!!!

Is there any way of setting the focus back to the next field to avoid this???




Rick Strahl
West Wind Technologies


from Maui, Hawaii

Making waves on the Web


Viewing all articles
Browse latest Browse all 10393

Trending Articles