How to: Handle Silverlight Screen Events


For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

You can customize your application by writing code that runs when certain events happen. For example, you can write code that runs before data is saved to a data source.

The events you can handle can be grouped into four categories:

  • Button methods that are called when a button is clicked.

  • General methods that are called when data is loaded or saved, or when a screen is closed.

  • Access control methods that are called to verify if a user has permission to perform a task.

  • Collection methods that are called when a collection is modified.

A description of these methods appears in the tables at the end of this topic.

To handle a screen event

  1. Open the screen that you want to modify by double clicking it in the Solution Explorer.

    The Screen Designer opens.

  2. At the top of the Screen Designer, click the arrow next to the Write Code button, and then select a method. The methods to which you can add custom code appear in the tables below.

    The Code Editor opens.

    System_CAPS_ICON_note.jpg Note

    The Collection Methods only appear in the Write Code drop-down list if the collection is selected from the left data pane in the Screen Designer.

  3. Place your cursor in the method that was just created and type the code that you want to run when the event occurs.

The following table lists screen-related event methods. All of these methods run on the client tier.

Button MethodsDescription
<MyMethodName>_ExecuteCalled when the button associated with the method is clicked.
General MethodsDescription
<ScreenName>_ActivatedCalled just after a screen is activated.
<ScreenName>_ClosingCalled just before the screen closes.
<ScreenName>_CreatedCalled just after the screen appears.
<ScreenName>_InitializeDataWorkspaceCalled just before the screen data is retrieved.
<ScreenName>_RunCalled when a request is made to display the screen.
<ScreenName>_SaveErrorCalled when attempting to save the screen results in an error.
<ScreenName>_SavedCalled just after the screen is saved.
<ScreenName>_SavingCalled just before the screen is saved.
Access Control MethodsDescription
CanRun<ScreenNamCalled before a screen appears. LightSwitch calls this method to check permissions for the current user.
<MyMethodName>_CanExecuteCalled before a method is runs. LightSwitch calls this method to check permissions for the current user.
Collection MethodsDescription
<CollectionName>_ChangedCalled just after the collection has changed.
<CollectionName>_SelectionChangedCalled just after the currently selected item in the collection is selected.

Screens: The User Interface of Your LightSwitch Application
How to: Handle Data Events
How to: Handle Query Events
How to: Modify a Silverlight Screen by Using Code