This topic is locked

How to: populate date field with current date after checkbox was selected

1/4/2016 2:01:36 PM
PHPRunner Tips and Tricks
Sergey Kornilov admin

We have two options here, populate date field right on the page and do that in one of server events.
In our examples checkbox field name is 'check' and date field name is 'datefield'.
[size="4"]1. Javascript example[/size]
When date field appears on the page it makes more sense to populate it right after check box field was checked off. Here is the code that you need to add to Add/Edit page Javascript OnLoad event:

d=new Date();
var ctrlCheck= Runner.getControl(pageid, 'check');

var ctrlDate = Runner.getControl(pageid, 'datefield');
ctrlCheck.on('change', function(e){

if (this.getValue() == 'on'){

ctrlDate.setValue(d);

}
});


Here is how it looks in generated application:


This code will work without any changes in ASPRunner.NET and ASPRunnerPro.
[size="4"]2. Server side example. [/size]
This example makes more sense if date field doesn't appear on Add/Edit pages and you need to perform this sort of change behind the scene.
Here is the code that can be added to BeforeAdd/BeforeEdit events:
PHPRunner code

if ($values["check"]==1)

$values["datefield"]=now();


ASPRunnerPro code

if values("check")=1 then

values("datefield")=now()

end if


ASPRunner.NET code (C#)

if (values["check"]==1)

values["datefield"]=DateTime.Now;