Published in


Lessons learned with LightSwitch HTML Client project


LightSwitch as a technology/tool has always impressed me.I was quite fascinated that how this super cool technology enables us to avoid all that plumbing code and concentrate on the real core busines

lessons learned

s logic. In a true sense it supports Rapid Application Development. I was already familiar with the Silverlight Client and had developed complete ERP application. LightSwitch team recently shipped the HTML Client with Visual Studio 2012 Update 2. When I first read about it , the first thought that came to my mind was that it can be a game changer of building LOB apps on mobile.It fully supports adaptive rendering and it based on the Single Page Application concept. I immediately decided that I will try my hands on it but wanted to do some real project rather than some small examples. I decided that I will try to convert one of my modules that were written in Silverlight client to HTML client. So I started the conversion. During the development phase I got stuck up but thanks to Huy Nguyen , from MS who helped me with it. So in this post I share the code snippets of the common scenarios that are required for any LOB apps. I assume if you are reading this post then you are already aware of the basics of LightSwitch client , if not then I recommend you to learn first and revisit this post.


It is one of the common requirements that when the form loads, to increase the usability of the application the cursor should be focused on the first element of the form. This will avoid the user an extra click of focusing the cursor on the control and he/she can immediately start typing. Here you need to target the post_render event of the textbox , of which you need to set focus. One important thing to note in the code snippet is that we are calling the focus method under settimeout method. This is just to ensure that the all the required elements are loaded in DOM.

  1. myapp.AddEditCustomer.CustomerName_postRender = function (element, contentItem) {
  2. // Write code here.
  3. var $firstTextBox;
  4. $firstTextBox = $(“input”, $(element)); //get the input tag
  5. setTimeout(function() {
  6. $firstTextBox.focus(); //set the focus
  7. }, 2);
  8. };


Imagine you are developing an order data entry screen , You have customer dropdown in this screen and every customer has a fixed percentage of discount associated to it. Now whenever the dropdown value changes you need to retrieve the discount value from the selected customer and do the processing of the discount calculations and update the final price read-only textbox on the screen. Let us see how you can achieve this.

  1. myapp.AddEditOrder.created = function (screen) {
  2. var order = screen.Orders;
  3. function updateTotal() {
  4. var price;
  5. var discount = 0;
  6. price = order.Item.Price;
  7. //if selected customer on order screen is not null
  8. if (order.Customer) {
  9. discount = order.Customer.DiscountType;
  10. }
  11. order.Total = price * (1 — discount / 100);
  12. }
  13. // When the order’s Customer changes, update its total by attaching the change event listener.
  14. order.addChangeListener(“Customer”, updateTotal);
  15. };


You have the screen of unapproved orders which is data binded to your custom query (where isApproved==false) of Orders entity. All this orders are displayed as Tiles. When any tile is selected a new screen(AddEditOrder) from where the user can set the approved flag equals true. When this screen closes the accepted behaviour should be that the parent screen should no more show that order, unfortunately LightSwitch cannot handle this automatically. And we need to write the custom code for this to get achieved. You cannot use the default EditSelected on the ItemTap event but write your own custom code for it.

  1. myapp.ApproveOrders.Orders_ItemTap_execute = function (screen) {
  2. // Write code here.
  3. myapp.showApproveOrdersScreen(screen.UnApprovedOrders.selectedItem, {
  4. afterClosed: function () {
  5. screen.UnApprovedOrders.load(); // manual refresh
  6. }
  7. });
  8. };


Cheers and Keep learning new stuff :)



A software geek and technocrat passionate about all emerging technologies. Desire to become a tech unicorn. Open Source Software Enthuiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store