This topic is locked

Hide/show fields based on checkbox

9/16/2016 3:46:02 PM
ASPRunner.NET General questions
Pete K author

Based on the JS API documentation, I wrote some code to show or hide two groups of controls on a multi-step add page. I have it working visually. However I must have done something wrong because one of the two groups fails to save to the database even when visible. Here is the code I added to the event:


pageObj.hideField("ReasonForWantingToServe");

pageObj.hideField("ProposedName");

pageObj.hideField("Rationale");
var ctrlVolunteer = Runner.getControl(pageid, 'Volunteer');

var ctrlSuggestName = Runner.getControl(pageid, 'SuggestName');
ctrlSuggestName.on('change', function(){

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

{

pageObj.showField("ProposedName");

pageObj.showField("Rationale");

}

else

{

pageObj.hideField("ProposedName");

pageObj.hideField("Rationale");

}

});



ctrlVolunteer.on('change', function(){

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

pageObj.showField("ReasonForWantingToServe");

}else{

pageObj.hideField("ReasonForWantingToServe");

}

});


As you can see, I want the checkboxes named 'Volunteer' and 'SuggestName' to toggle the respective sections. As I said, visually it works. But upon saving, I find that the value for "ReasonForWantingToServe" is always saved to the database if populated, while "ProposedName" and "Rationale" are never saved even if visible and filled in. SQL trace reveals that those two fields are not added to the save SQL.
What's wrong with my code?

A
Arkie 9/16/2016

I'm very interested in how this turns out. I was doing something along this line and couldn't get the check mark bit to work the way I expected it to. I wound up using another feature like this:

if (confirm('some statement here ??'))

It puts up a box for a yes/no response. So if the response is Yes, my system does something akin to:

var ctrlName = Runner.getControl(pageid, 'Name');

var ctrlNewName = Runner.getControl(pageid, 'NewName');

function func()

{

ctrlNewName.setValue(ctrlName.getValue());

};

ctrlName.on('keyup', func);
And if the response is No, it simply falls through to the next thing. and it all works as expected.
HTH... ~Joe

jadachDevClub member 9/16/2016

Peter, if you comment out the JavaScript, then add data to "ProposedName" and "Rationale" - does the data stick to the database table?

Pete K author 9/19/2016



I'm very interested in how this turns out. I was doing something along this line and couldn't get the check mark bit to work the way I expected it to. I wound up using another feature like this:

if (confirm('some statement here ??'))

It puts up a box for a yes/no response. So if the response is Yes, my system does something akin to:

var ctrlName = Runner.getControl(pageid, 'Name');

var ctrlNewName = Runner.getControl(pageid, 'NewName');

function func()

{

ctrlNewName.setValue(ctrlName.getValue());

};

ctrlName.on('keyup', func);
And if the response is No, it simply falls through to the next thing. and it all works as expected.
HTH... ~Joe


Thanks Joe. That's an interesting approach.

Pete K author 9/19/2016



Peter, if you comment out the Javascript, then add data to "ProposedName" and "Rationale" - does the data stick to the database table?


Yes it does, Jerry. It seems that something about my JS code is causing those fields to not get saved.

jadachDevClub member 9/19/2016



Yes it does, Jerry. It seems that something about my JS code is causing those fields to not get saved.


That's strange. Below is code I am using and it does work. Maybe something here can help. Do you have anything happening before record added event?
var ctrlReasonID = Runner.getControl(pageid, 'ReasonID');

var ctrlTransferEmployee = Runner.getControl(pageid, 'TransferEmployee');

var ctrlTransferDepartment = Runner.getControl(pageid, 'TransferDepartment');
pageObj.hideField("TransferEmployee");

pageObj.hideField("TransferDepartment");
ctrlReasonID.on('change', function(e){
if (this.getValue() == '6'){

pageObj.showField("TransferEmployee");

ctrlTransferEmployee.addValidation("IsRequired");

pageObj.showField("TransferDepartment");

ctrlTransferDepartment.addValidation("IsRequired");
}
else{

pageObj.hideField("TransferEmployee");

ctrlTransferEmployee.removeValidation("IsRequired");

pageObj.hideField("TransferDepartment");

ctrlTransferDepartment.removeValidation("IsRequired");
}
});

A
Arkie 9/19/2016

hmmmm.... Is the only substantive part that's different is the

if (this.getValue()==
This is what I had a problem with. A check box versus a selected value (like your 6).
Whatcha think Pete?
~Joe

jadachDevClub member 9/19/2016



hmmmm.... Is the only substantive part that's different is the

if (this.getValue()==
This is what I had a problem with. A check box versus a selected value (like your 6).
Whatcha think Pete?
~Joe


if (this.getValue() == 'on')
also works for me when using bit fields.

Pete K author 9/20/2016



also works for me when using bit fields.


Yep. The test is working, as the fields are shown/hidden as expected. It's just that even when shown, one group does not get saved. The biggest difference between my code and yours is that I'm using two separate functions due to having two checkboxes to test. I did notice that you are passing a parameter (e) while neither of mine passes a parameter. What is the significance of e and should I be doing something similar?

jadachDevClub member 9/20/2016



Yep. The test is working, as the fields are shown/hidden as expected. It's just that even when shown, one group does not get saved. The biggest difference between my code and yours is that I'm using two separate functions due to having two checkboxes to test. I did notice that you are passing a parameter (e) while neither of mine passes a parameter. What is the significance of e and should I be doing something similar?


I guess I picked up the (e) here:
http://xlinesoft.com/asprunnernet/docs/show_dropdown_list_of_us_states.htm