RadGrid for ASP.NET AJAX

RadControls for ASP.NET AJAX

In various situations you may want to detect whether the user is currently editing grid item or performs an insert operation. This is useful if you would like to have different appearance for the edit form on item insertion than that on item editing (suitable for WebUserControl or FormTemplate custom edit form). For example, you may want to hide primary key field from the form or change update button text to insert on initial insert. These online example demonstrates the second functionality:

http://demos.telerik.com/aspnet-ajax/Grid/Examples/DataEditing/TemplateFormUpdate/DefaultCS.aspx

http://demos.telerik.com/aspnet-ajax/Grid/Examples/DataEditing/TemplateFormUpdate/DefaultVB.aspx

And the code extractions are:

CopyC#
<asp:Button ID="btnUpdate" Text='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "PerformInsert" : "Update" %>'>
CopyVB.NET
<asp:Button ID="Button1" Text='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "Insert", "Update") %>'
runat="server" CommandName='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "PerformInsert", "Update") %>'>

The IsItemInserted boolean property of the GridTableView is especially designed for this type of cases. You can check its value in the ItemCreated/ItemDataBound handlers of the grid to verify whether the item is in edit or insert mode. Then you can modify the edit form appearance(on ItemCreated) or edit form controls values (on ItemDataBound) if needed. Below are sample code snippets (applicable for WebUserControl and FormTemplate custom editor types):

For more information concerning the major differences between ItemCreated and ItemDataBound events please read this article. You can also learn how to control the edit/insert/regular modes in the grid (to prevent unexpected results when both edit and insert form is opened) from this topic.