Prevent RadGrid Reload On Insert

1 Answer 164 Views
Ajax Grid
Vincent
Top achievements
Rank 1
Vincent asked on 30 Aug 2022, 04:31 PM

Hi,

 

I have a RadGrid consisting of a number of GridTemplateColumn's containing InsertTemplateColumns. When an item is inserted I perform validation on the C# side and either perform the insert or display an error message with RadWindowManager and RadAlert. The problem is when validation fails and an error message is displayed the grid is reloaded and all of the insert input fields are emptied, forcing the user to re-enter all the information. How can I validate the insert and display an error message without reloading the grid?

Thanks

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 02 Sep 2022, 10:34 AM

Hi Vincent,

I've already answered your support ticket, but I will share this information in this forum thread to make it available to everyone.

 

The reason the values are lost is that clicking the Update/Insert buttons will trigger the Update/Insert command and they rebind the Grid. For more details, you can check out the Commands that invoke Rebind Implicitly

If by the time the Grid rebinds the data is not updated, every value will be replaced with the data from the database.

You can, however, subscribe to the Grid's Server Events that trigger upon Inserting/Updating, validate the form input and cancel the event if there are discrepancies.

The events can be individual for each action (InsertCommand, UpdateCommand), or a common event triggered for all actions (ItemCommand).

 

For example, the approach would look like this when inserting:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    // Validate the Form

    // Condition to check if valid
    if (!valid)
    {
        // Cancel the event
        e.Canceled = true;

        // Show notifications
    }else
    {
        // Insert the record
    }
}

 

 

when Updating:

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    // Validate the Form

    // Condition to check if valid
    if (!valid)
    {
        // Cancel the event
        e.Canceled = true;

        // Show notifications
    }else
    {
        // Update the record
    }
}

 

Or if you use the ItemCommand event

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if(e.CommandName == RadGrid.PerformInsertCommandName)
    {
        // When inserting

        // Validate the Form

        // Condition to check if valid
        if (!valid)
        {
            // Cancel the event
            e.Canceled = true;

            // Show notifications
        }
        else
        {
            // Insert the record
        }
    }
    else if(e.CommandName == RadGrid.UpdateCommandName)
    {
        // When updating

        // Validate the Form

        // Condition to check if valid
        if (!valid)
        {
            // Cancel the event
            e.Canceled = true;

            // Show notifications
        }
        else
        {
            // Update the record
        }
    }
}

 

For accessing Controls in RadGrid, you can follow the instructions and examples from the Accessing Controls in RadGrid.

 

You can also check out the following Blog Post that shares a few tricks to debug the application on the server side: 6 Server-Side Debugging Tips That Will Make Developing Easier


Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Ajax Grid
Asked by
Vincent
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or