New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Customize the Insertion Form Display Position

There are cases when you may prefer to display the insertion form above/below the active grid page or visualizethe form on the current/first grid page instead on the last page (which is the default behavior on init insert). Furthermore, it would be convenient for the end user to keep the current page index when pressing the [Cancel] button from the insertion form.This is a pretty straight-forward task using the InsertItemDisplayand InsertItemPageIndexAction properties of the GridTableViewobject. here are the possible values for these properties:InsertItemDisplay: Top (default value) or BottomInsertItemPageIndexAction: ShowItemOnCurrentPage, ShowItemOnLastPage (default value) or ShowItemOnFirstPageThe usage of these properties is illustrated in this example.If you prefer to retain the the active page on insert action with coding, subscribe to the ItemCommand event of RadGrid. The steps you have to undertake are:

  1. Cancel the default action on init insert and store the current page index

  2. Process the insert invoking the InsertItem() method, after that recover the page index and rebind the grid

  3. Intercept the cancel command when the insert form is visible and keep the active page displayed

The code snippets below represent a sample implementation:

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True"
  Skin="Silk" runat="server" GridLines="None" Width="95%" AllowPaging="true">
  <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" />
  <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    If (e.CommandName = RadGrid.InitInsertCommandName) Then
        e.Canceled = True
        Session("curPageIndex") = e.Item.OwnerTableView.CurrentPageIndex
        e.Item.OwnerTableView.InsertItem()

        e.Item.OwnerTableView.CurrentPageIndex = Integer.Parse(Session("curPageIndex"))
        e.Item.OwnerTableView.Rebind()
    ElseIf (e.CommandName = RadGrid.CancelCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
        e.Item.OwnerTableView.CurrentPageIndex = Integer.Parse(Session("curPageIndex"))
    End If
End Sub
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        e.Canceled = true;
        Session["curPageIndex"] = e.Item.OwnerTableView.CurrentPageIndex;
        e.Item.OwnerTableView.InsertItem();

        e.Item.OwnerTableView.CurrentPageIndex = int.Parse(Session["curPageIndex"]);
        e.Item.OwnerTableView.Rebind();
    }
    else if (e.CommandName == RadGrid.CancelCommandName && e.Item.OwnerTableView.IsItemInserted)
    {
        e.Item.OwnerTableView.CurrentPageIndex = int.Parse(Session["curPageIndex"]);
    }
}
In this article