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):
CopyC#
private void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if(e.Item is GridEditableItem && e.Item.IsInEditMode)
{
if(e.Item.OwnerTableView.IsItemInserted)
{
}
else
{
}
}
}
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if(e.Item is GridEditableItem && e.Item.IsInEditMode)
{
if(e.Item.OwnerTableView.IsItemInserted)
{
}
else
{
}
}
}
CopyVB.NET
Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
If e.Item.OwnerTableView.IsItemInserted Then
Else
End If
End If
End Sub
Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
If e.Item.OwnerTableView.IsItemInserted Then
Else
End If
End If
End Sub
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.