Distinguish Edit or Insert Mode
In various situations, you may want to detect whether the user is editing a grid item or performing an insert operation. This is useful when you want different appearances for the edit form during insertion and editing, such as with a WebUserControl or FormTemplate custom edit form. For example, you may want to hide the primary key field or change the Update button text to Insert during the initial insert. These online examples demonstrate the second functionality:
VB.NET edit and insert form demo
And the code extractions are:
<asp:Button ID="btnUpdate" Text='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "PerformInsert" : "Update" %>'>
<asp:Button ID="Button1" Text='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "Insert", "Update") %>'
runat="server" CommandName='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "PerformInsert", "Update") %>'>
You can check the type of the editable item in the ItemCreated or ItemDataBound handlers of the grid to verify whether the item is in edit or insert mode. Then you can modify the edit form appearance in ItemCreated or the edit form control values in ItemDataBound. Below are sample code snippets:
private void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if(e.Item is GridEditableItem && e.Item.IsInEditMode)
{
if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
{
// insert item
}
else
{
// edit item
}
}
}
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
{
// insert item
}
else
{
// edit item
}
} For more information about the differences between the ItemCreated and ItemDataBound events, see Differences between ItemCreated and ItemDataBound. You can also learn how to control the edit, insert, and regular modes in the grid from Controlling insert and edit modes.