Close edit window

0 Answers 99 Views
DataGrid
Terry
Top achievements
Rank 1
Terry asked on 22 Mar 2023, 12:02 PM

We use a Telerik RadGrid to display data from our tables on a page. The page is done in old C# .NET Framework 4.8.  Our Telerik version is 2023.1.117.45.

The page allows the user to edit the data from the DB. When they click a button the page displays an edit control with the data from a given row that has a save button and a cancel button. When the cancel button is hit the RadGrid sends a "Cancel" command to the code-behind, even though nothing in the code-behind responds to this command the edit form is removed.

On some of our other pages when the save button is hit it runs the update/insert code behind, and also removes the control. But I recently added a new page, based upon the old one, but the new one does not remove the edit control when a save it done. My code on the new page is obviously slightly different, but I cannot find what I changed that stops the removal of the edit form once the data is saved. The cancel button on this new page does this, but again, neither page has any code-behind that responds to the cancel button.

Is there documentation on how the cancel command works? It seems to be done auto-magically by Telerik without the code added by the developer running. Is there an explicit way to trigger this effect?

My previous experience doing front end stuff was using JavaScript and in that I would just edit the dom to remove the control, or hide it. But that is never done on any of the page using Telerik here.

Dilyan Traykov
Telerik team
commented on 27 Mar 2023, 07:55 AM

Hello Terry,

The Execute method of the default CommitEditCommand and CancelEditCommand does invoke some logic that hides the flyout used to display the external editor. Here is the code of interest:

                    if (this.Owner.UserEditMode == DataGridUserEditMode.External)
                    {
                        var id = this.Owner.ExternalEditor.Position == ExternalEditorPosition.Left ? DataGridFlyoutId.EditorLeft : DataGridFlyoutId.EditorRight;
                        this.Owner.ContentFlyout.Hide(id);

                        if (trigger != ActionTrigger.ExternalEditor)
                        {
                            this.Owner.ExternalEditor.CommitEdit();
                        }
                    }

To ensure this happens in any custom implementations you provide, you can call the following method as explained in the CancelEdit Command article:

this.Owner.CommandService.ExecuteDefaultCommand(CommandId.CancelEdit, context);
Similarly, you can call the ExecuteDefaultCommand method by passing the CommandId.CommitEdit parameter.

Please let me know if doing so provides the expected results. If that is not the case, please demonstrate your setup and requirement in a small sample project and I will gladly assist you further.

Terry
Top achievements
Rank 1
commented on 27 Mar 2023, 01:22 PM

Thank you Dilyan,

 

I am not sure if the code snippet you included applies to what I am facing. I have a RadGrid that calls the code behind method below when certain buttons are pushed. Depending on the command passed to this method different functions will happen. For some of them (DetailInsert and Update) I want the edit control to be removed from the edit control once the operation succeeds.

There are several classes, but in the one below the edit control is ItemCatMaintDetail; which extends a System.Web.Ui.UserControl class. The UpdateHeader method is there to alert the user of the success or failure of the update and insert commands. If either type of command is a success I want the edit form removed.


protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string error = null;
            try
            {
                if (e.Item != null)
                {
                    ItemCatMaintDetail Ctrl = ((ItemCatMaintDetail)e.Item.FindControl(GridEditFormItem.EditFormUserControlID));

                    switch (e.CommandName)
                    {
                        case "DetailInsert":
                            if (!Page.IsValid || Ctrl.Values == null)
                                return;
                            error = DetailInsert(e, myHeader);
                            
                            break;
                        case "Copy":
                            error = CopyItem(e);
                            break;
                        case "Update":
                            if(!Page.IsValid || Ctrl.Values == null)
                                return;
                            error = UpdateItem(e, myHeader);
                            break;
                        case "Delete":
                            var deleteResponse = _dbAdapter
                                                 .DeleteItemMaintAsync(new ItemCatMaintDetail()
                                                 .PageDataValues((GridDataItem)e.Item));
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            // TODO : redesign to take a process result.
            // We alert the user on every error, but only alert on success for insert and update.
            if (error != null)
                UpdateHeader(myHeader, error);
        }


 

Dilyan Traykov
Telerik team
commented on 28 Mar 2023, 11:14 AM

Hello Terry,

Thank you for the provided code snippet and explanation.

Would it be possible to call the CommitEdit and CancelEdit methods inside the body of the RadGrid1_ItemCommand method in such a case? Something like this:

case "DetailInsert":
    if (!Page.IsValid || Ctrl.Values == null)
        return;
    error = DetailInsert(e, myHeader);
    RadDataGrid dataGrid = sender as RadDataGrid;
    dataGrid.CommitEdit();

And:

case "Update":
    if(!Page.IsValid || Ctrl.Values == null)
        return;
    error = UpdateItem(e, myHeader);
    RadDataGrid dataGrid = sender as RadDataGrid;
    dataGrid.CommitEdit();

If this does not work for you, please demonstrate your scenario in an isolated project so that I can better assist you in achieving the desired result.

Terry
Top achievements
Rank 1
commented on 29 Mar 2023, 12:34 PM

Hello Dilyan,

I appreciate the quick response. I will have to see if this works as well. But I did find something else that seems to have worked. If I set a property called "IsItemInserted" to false that seems to kill the edit/insert detail window. Not sure if this is the "Kosher" way to do it, though. I use the command as follows


case "Update":
    if(!Page.IsValid || Ctrl.Values == null)
        return;
    error = UpdateItem(e, myHeader);
    e.Item.OwnerTableView.IsItemInserted

Dilyan Traykov
Telerik team
commented on 30 Mar 2023, 12:28 PM

Hello Terry,

I'm glad to hear you've found an alternative approach for achieving the desired result. If this does work for you, you can keep this setter, but I would suggest also testing the approach I proposed. Do let me know how this goes.

No answers yet. Maybe you can help?

Tags
DataGrid
Asked by
Terry
Top achievements
Rank 1
Share this question
or