MS Visual Studio Lightswitch flipswitch control sometimes does have displayed default value

125 Views Asked by At

I have an interesting issue with a LightSwitch app I am maintaining, there is room for confusion so will try and explain at length and answer any clarifying questions.

I have a LightSwitch AddEdit screen using the default 'Flip Switch' control for Boolean values in a table, set to Yes/No Option (so shows Yes or No as values, actually saves 0 or 1), when creating a new entry, the control shows 'No' by default. The issue is that depending on what permission the user has the control behaves differently, either:

1) the expected/desired behaviour: The flip switch is set to 'No' by default and if the screen is saved without touching it 'No' is saved to the database (What the user sees and what is saved is consistent, a flip switch is never NULL).

2) The unexpected behaviour: The flip switch looks like it is set to 'No', but when user attempts to save the screen, gets a warning "Error: Cannot continue. There are validation errors on the page" because there are Boolean values that do not allow NULL (the relevant flip switches are highlighted with 'This field is required') the user can toggle the flip switch control to Yes and back to No, at which point the screen will save (touching the control seems to set it to a value from NULL), but disturbingly the other flip switches that do allow NULL are saved as NULL instead of the 'No' they show on the screen, this is confusing and problematic and will require users to re-enter all the NULL entries once I fix this bug.

The troubleshooting I had done so far shows that the behaviour is contingent on what LightSwitch Access Control permission the user has set, if they are in one group(SystemAdministrator) they get behaviour 1 in another(DatabaseOperators) they get 2.

It also appears contingent on being an 'add' rather than an 'edit' if adding a new row as a SystemAdministrator, the data will be added as behaviour 1. if editing an existing entry you get behaviour 2 as both permissions.

I tested commenting out the VB code that filters the permissions for the data source for this table (TableName_CanRead, TableName_CanWrite, TableName_CanInsert, TableName_CanUpdate, TableName_CanDelete, TableName_CanExecute, TableName_Filter) as that seemed to be a likely point of difference and it did not change the behaviour.

I am hoping that someone more familiar with the LightSwitch environment will be able to point me to another likely place where this issue is coming from and give some hints on where to look or further troubleshooting steps. I currently can't see how the permission group is effecting the flip switch control behaviour so markedly

1

There are 1 best solutions below

11
Crezzer7 On

The best practice for your scenario in my opinion is the following:

on the screen created code block, use the following code for each switch:

myapp.SCREENNAME.created = function (screen) {
    if (screen.TABLENAME.theFlipSwitchA == null) {
        screen.TABLENAME.theFlipSwitchA = false;
    }

    if (screen.TABLENAME.theFlipSwitchB == null) {
        screen.TABLENAME.theFlipSwitchB = true;
    }
}

This will ensure the null values are replaced with the correct TRUE or FALSE values at the start, and will only change if the user changes them.

In Addition

There are 2 views available for a flip switch aswell being Yes/No and On/Off, which can be accessed on the properties of the selected Flip Switch:

enter image description here

One thing to make sure is you use the Data Binding property to specify the value, not the Name like you usually would on a forms application for example.

enter image description here