Search code examples
parameter-passingdefault-valuevisual-studio-lightswitch

Pass Data between screens with Lightswitch HTML client


My application starts with a Browse screenwhere you choose an employee. You are then brought to another Browse screenwhich displays records for that employee. On the second browse screen I have a button which opens an Add/Edit screen.

I would like to default a field on my Add/Edit screen to be the EmployeeID which was selected on the first Browse screen

My application uses views to populate the Browse screens with distinct lists, but the Add/Edit references the table directly. For this reason I have been unable to create a relationship between my application DataSources

Currently I can default the CreateDate value with the code below but the second line is not returning the EmployeeID as I want

myapp.AddTimesheetRecord.created = function (screen) {
    // Write code here.
    screen.TBG_KeepInTimesheet.CreateDate = new Date;
    screen.TBG_KeepInTimesheet.EmployeeID = myapp.activeDataWorkspace.WPEBrattleData.TBG_V_KeepInTimeSheet_Details.EmployeeID;
};

Here is an Image of my Add/Edit screen designer, note that currently EmployeeID will be blank when run with the code above

Here is an Image of my second Browse screen designer


Solution

  • One option would be to edit the tap action of your AddNewRecord Command Bar Button and use the 'Write my own method' to implement a AddNewRecord_Tap function along the following lines:

    myapp.ViewEmployeeProjects.AddNewRecord_Tap_execute = function (screen) {
        myapp.showAddTimesheetRecord(null, {
            beforeShown: function (addScreen) {
                var newItem = new myapp.TBG_KeepInTimesheet();
                newItem.EmployeeID = screen.TBG_V_KeepInTimeSheet_Employee.EmployeeID;
                addScreen.TBG_KeepInTimesheet = newItem;
            }
        });
    };
    

    This approach allows you to use the selected employee id (from your Browse screen) to initialise your new time-sheet record prior to showing your AddEdit screen.