My previous article described how to get the selected item from ListView and how to programmatically delete the selected item permanently in LightSwitch Beta 2. Today we shall see how to display a screen with records retrieved from a database based on the parameter passed to the screen.
Need of passing parameter to Screen:
When we need to display a particular set of records to the user at the start-up of a screen then we have to use a parameterized query in the screen.
Let us consider, we have two screens in which one is used to display the single value [summary property] on selecting that it will display the other screen with the details of the selected single value.
To demonstrate this, we shall create a demo LightSwitch Desktop application.
Preparing the Solution:
Fire up the VS 2010; create a LightSwitch Desktop application with the name "PassingParameterToScreenInLSBeta2" as shown in the figure.
Follow the No: # to create the Desktop LightSwitch application.
The Entity Designer:
Let us create a Table.
This is a new screen of Beta 2 which is not available in Beta 1. Select the rounded option to create a new table.
After clicking on the "Create new table" option, the Entity Designer will be displayed to create the Entity required. In this demo application we create the table called "Person".
To change the Table name simply click on the Header of the Entity Designer and give the new name.
Screens to application:
For the demo application we need to create three screens.
- CreateNewPerson - to create a person record.
- StatesListDetails - to display the states.
- PeopleListDetailsBasedOnState - Based on selection of a state, the People details will be displayed for the selected state.
Adding New Data Screen:
To add new data screen:
- Select "New Data Screen" template from Screen template panel.
- Select the "Person" entity as Screen Data.
- Click OK to proceed.
Adding StatesListDetails Screen:
This screen is based on the "List and Details Screen" template.
- Select the "List and Details Screen" template.
- Name it as "StatesListDetails".
- Select the "Person Details" as Screen Data.
- Click OK to proceed.
This is the screen which lists all the States. To display only the state value to the user we need to change the screen layout somewhat.
- Delete the "Add", "Edit" and "Delete" buttons. Because we don't need to add or edit or delete the states.
- Change the Layout. We have changed from Column layout to Rows layout.
- Delete all the fields except "State". Because we don't want to show all the fields except state.
- Delete the "Details Column". Of course we don't need to display the details too.
Add a new button to the screen and name it "GetPeopleListFromState".
Change some properties so that the screen will appear in the middle of the application.
Before editing the button method, we have to create a screen which accepts the parameter.
Adding "PeopleListDetailsBasedOnState" Screen:
This is the screen which accepts the parameter passed from other screens. To display the records based on the passed parameter, we need to create a screen based query which has the criteria that looks for a parameter.
Follow the No: # to create the screen.
Creating Screen Based Query:
We have discussed about this in one of my previous articles. Please read that article to know about Screen Based query.
Create a query as shown in the figure. Select Parameter from dropdownlist and Click on "Add New..." to create a parameter.
After clicking on "Add New..." we wil see a parameter created in the parameter section. Just change the name to "StateName".
Creating Screen Parameter:
To add the Screen parameter, Click on the "Add Data Item..." from the top of the query designer.
Then change the Select the "Local Property" option and change the Property name to "StateName".
Make sure the property we created is a screen parameter.
In the StateName property, select the "Is Parameter" check box so that LightSwitch makes the screen as a parameterised screen.
As shown in the figure now we can see two parameters in the People entity details panel.
The StateName property which is highlighted in yellow color is a Query parameter and the brown color is a Screen parameter.
Hooking the Properties:
We have the screen and query parameter here. To pass the value to query parameter which is passed to screen parameter from other screens, we need to hook the Screen parameter with query parameter.
Select the Query parameter, go to the property page; simply click on the "Parameter Binding" text box; we will have a dropdownlist. Select "StateName" from the list.
Make sure the parameters are hooked. See the People entity panel; a line will be drawn from the Query parameter to Screen parameter.
Passing Parameter to Screen:
The highlighted part shows you the state value which is going to the "PeopleListDetailsBasedOnState" screen as parameter.
That's it! We have done.
Press F5 to see the application in action.
Application in Action:
Add some records using the Create New Person screen.
- Select any one item from list view. We have selected TN State.
- Click on the "Get People List From State" to get the details of the persons who are based on TN.
As we have selected the TN value is passed to as a parameter to the screen.
After clicking on the button, we will be directed to the list and details page of the people who are in TN.
In this article, we have seen how to pass the parameter to a screen in a LightSwitch application.
Thanks for spending your precious time here. Please provide your valuable feedback and comments, which enable me to give a better article the next time.