You are here:   Blog
Register   |  Login

 

Jan 6

Written by: Michael Washington
1/6/2013 6:57 PM  RssIcon

Automatic Save (And Refresh)

image

At the time of this writing, the LightSwitch HTML Client is still in preview. We can expect additional functionality in the future release. For now, any updates to the data require the user to explicitly press the save button. This is not optimal in all situations.

For example, we now desire the ability to delete all the sales records for a single sales person, then automatically switch back to the main page. To implement this, we can use the method described in the article Retrieving The Current User In The LightSwitch HTML Client.

image

First we switch to File View.

image

 

We then add a page to the Server project using the following code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LightSwitchApplication.Web
{
    public class DeleteRecords : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Get the LightSwitch serverContext
            using (var serverContext = ServerApplicationContext.CreateContext())
            {
                // Minimal security is to check for IsAuthenticated
                if (serverContext.Application.User.IsAuthenticated)
                {
                    if (context.Request.QueryString["SalesPersonId"] != null)
                    {
                        // The Salesperson was passed as a parameter
                        // Note that better security would be to check if the current user
                        // should have the ability to delete the records
                        // 'serverContext.Application.User.Name' returns the current user
                        int intSalesPersonId = Convert.ToInt32(context.Request.QueryString["SalesPersonId"]);
                        // Get the Sales records to delete
                        var result = from Sales in serverContext.DataWorkspace.ApplicationData
                                         .SalesSet.GetQuery().Execute()
                                     where Sales.SalesPerson.Id == intSalesPersonId
                                     select Sales;
                        // Loop through each record found
                        foreach (var item in result)
                        {
                            // Delete the record
                            item.Delete();
                            // Save changes
                            serverContext.DataWorkspace.ApplicationData.SaveChanges();
                        }
                        // Return a response
                        // We could return any potential errors
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("complete");
                    }
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

(this code uses the new serverContext API)

 

image

We switch back to Logical View, open the AddEditSalesPerson screen, add a button, then select Edit Execute Code for the button.

 

We use the following code:

 

myapp.AddEditSalesPerson.DeleteAllSales_execute = function (screen) {
    // Get selected SalesPersonId
    var SalesPersonId = screen.SalesPerson.Id;
    $.ajax({
        type: 'post',
        data: {},
        url: '../web/DeleteRecords.ashx?SalesPersonId=' + SalesPersonId,
        success: function success(result) {
            // Navigate back to main page
            myapp.navigateBack();
        }
    });
};

 

image

When we run the application, we can add sales for a sales person, then simply click the DELETE ALL button…

image

…the records will be deleted, and we will be automatically navigated back to the main page.

 

LightSwitch Help Website Articles

Creating A Desktop Experience Using Wijmo Grid In LightSwitch HTML Client

Creating ASP.NET Web Forms CRUD Pages Using ServerApplicationContext

Using Promises In Visual Studio LightSwitch

Retrieving The Current User In The LightSwitch HTML Client

Writing JavaScript That Implements The Binding Pattern In Visual Studio LightSwitch

Implementing The Wijmo Radial Gauge In The LightSwitch HTML Client

Writing JavaScript In LightSwitch HTML Client Preview

Creating JavaScript Using TypeScript in Visual Studio LightSwitch

Theming Your LightSwitch Website Using JQuery ThemeRoller

Using Toastr with Visual Studio LightSwitch HTML Client (Preview)

 

LightSwitch Team HTML and JavaScript Articles

Custom Controls and Data Binding in the LightSwitch HTML Client (Joe Binder)

Creating Screens with the LightSwitch HTML Client (Joe Binder)

The LightSwitch HTML Client: An Architectural Overview (Stephen Provine)

Writing JavaScript Code in LightSwitch (Joe Binder)

New LightSwitch HTML Client APIs (Stephen Provine)

A New API for LightSwitch Server Interaction: The ServerApplicationContext

Building a LightSwitch HTML Client: eBay Daily Deals (Andy Kung)

 

Special Thanks

A special thanks to LightSwitch team members Joe Binder and Stephen Provine for their valuable assistance.

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

(you must have HTML Client Preview 2 or higher installed to run the code)


Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation