New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Editing Switches directly in Batch Edit Mode

Environment

ProductTelerik WebForms Grid for ASP.NET AJAX

Description

How can I edit the Switches directly in Batch Edit Mode similarly to the example from the Editing CheckBoxes directly in Batch Edit Mode article?

"Editing Switches directly in Batch Edit Mode"

Solution

The EditMode="Batch" is a Client-side Editing functionality where everything is handled on the Client-side. For more details, you can refer to the following articles:

The BatchEditing functionality supports basic components such as CheckBox, TextBox out of the box. When working with Controls, such as the Telerik WebForms Switch the editing functionality needs to be overridden, see Working With Templates.

Markup

ASP.NET
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnBatchEditCommand="RadGrid1_BatchEditCommand">
    <MasterTableView AutoGenerateColumns="False" EditMode="Batch">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="IsActive" DataField="IsActive" HeaderText="RadSwitch">
                <ItemTemplate>
                    <telerik:RadSwitch ID="DisplaySwitch" runat="server" AutoPostBack="false" Checked='<%# Eval("IsActive") %>' OnClientClicking="OnClientClicking" Enabled="false"></telerik:RadSwitch>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadSwitch ID="EditorSwitch" runat="server" AutoPostBack="false"></telerik:RadSwitch>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <ClientEvents OnBatchEditGetCellValue="GetCellValue" />
        <ClientEvents OnBatchEditSetCellValue="SetCellValue" />
        <ClientEvents OnBatchEditGetEditorValue="GetEditorValue" />
        <ClientEvents OnBatchEditSetEditorValue="SetEditorValue" />
    </ClientSettings>
</telerik:RadGrid>
  • Override the Batch Editing events to handle the Switch component
JavaScript
function GetCellValue(sender, args) {
    if (args.get_columnUniqueName() == "IsActive") {
        args.set_cancel(true);
        var container = args.get_container();
        var displaySwitch = $telerik.findControl(container, "DisplaySwitch");
        args.set_value(displaySwitch.get_checked());
    }
}
function SetCellValue(sender, args) {
    if (args.get_columnUniqueName() == "IsActive") {
        args.set_cancel(true);
        var container = args.get_container();
        var displaySwitch = $telerik.findControl(container, "DisplaySwitch");
        displaySwitch.set_checked(args.get_value());
    }
}
function GetEditorValue(sender, args) {
    if (args.get_columnUniqueName() == "IsActive") {
        args.set_cancel(true);
        var container = args.get_container();
        var editorSwitch = $telerik.findControl(container, "EditorSwitch");
        args.set_value(editorSwitch.get_checked());
    }
}
function SetEditorValue(sender, args) {
    if (args.get_columnUniqueName() == "IsActive") {
        args.set_cancel(true);
        var container = args.get_container();
        var editorSwitch = $telerik.findControl(container, "EditorSwitch");
        editorSwitch.set_checked(args.get_value());
    }
}
  • Finally, In the OnClientClicking Client-side event of the Switch, cancel the event in order to prevent changing the checked state (the BatchEditing will take care of that) and using the BatchEditing APIs to change the cell value to the new checked state value.
JavaScript
function OnClientClicking(sender, args) {
    args.set_cancel(true);

    var displaySwitch = sender;
    var grid = $find("<%= RadGrid1.ClientID %>");
    var batchManager = grid.get_batchEditingManager();
    var parentCell = $telerik.$(displaySwitch.get_element()).closest("td")[0];

    var valueToSet = !displaySwitch.get_checked();

    batchManager.changeCellValue(parentCell, valueToSet);
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support