Search code examples
c#asp.netwebformsteleriktelerik-grid

Overriding the Telerik RadGrid AddNewRecordButton functionality to redirect the user to a new page rather than add a new insert record row to the grid


Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "InitInsert")
    {
        Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
    }
}

After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?


Solution

  • So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:

    Add this to your RadGrid1 attributes:

    OnItemCreated="RadGrid1_ItemCreated"
    

    Code behind (note: there is actually a Button AND a LinkButton):

    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
            //This is the icon with the plus (+) sign unless you've changed the icon
            Button iconButton = e.Item.FindControl("AddNewRecordButton");
            if (iconButton != null) {
                iconButton.OnClientClick = "";
            }
            //This is the words "Add New Record" or whatever you've called it
            LinkButton wordButton = e.Item.FindControl("InitInsertButton");
            if (wordButton != null) {
                wordButton.OnClientClick = "";
            }
        }
    }
    

    This should allow the postback to happen and the code you posted should be able to run.