覆盖 Telerik RadGrid AddNewRecordButton 功能以将用户重定向到新页面而不是向网格添加新的插入记录行

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

以前,当我的 RadGrid 不是批量编辑网格时,我可以使用网格的 AddNewRecord 按钮将用户重定向到另一个页面,代码如下:

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

在我将网格设为批量编辑网格后,“添加新按钮”不再进入 ItemCommand 事件,而是尝试向网格添加内联插入记录行。无论如何,我仍然可以使用此按钮并覆盖其功能以仍然重定向用户吗?

所以我测试了这个并证实了我在评论中的怀疑。当 EditMode="Batch" 时,"Add New Record" 按钮和其他按钮不再导致回发。您可以通过删除 RadGrid1_ItemCreatedOnClientClick 的 JavaScript 来覆盖它,如下所示:

将此添加到您的 RadGrid1 属性中:

OnItemCreated="RadGrid1_ItemCreated"

后面的代码(注意:实际上有一个Button和一个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 = "";
        }
    }
}

这应该允许回发,并且您发布的代码应该能够 运行。