After I edit/insert a record in my grid, I want the edit template to go away. I want this to happen for two reasons. 1) Once the user edits/inserts the record, there's no need for that edit/insert row to remain. 2) The template has a required field validator on it and when the user posts back in another part of the page the required field validator gets triggers on the field in this template.
I've tried MyGroupsRadGrid.MasterTableView.ClearEditItems() after the insert logic on MyGroupsRadGrid_InsertCommand() but that doesn't work. I've tried e.Canceled = true but that doesn't work. I've tried e.Item.Edit = false but that errors.
<telerik:RadGrid ID="MyGroupsRadGrid" runat="server" OnNeedDataSource="MyGroupsRadGrid_NeedDataSource" AutoGenerateColumns="false" AutoPostBackOnFilter="true" OnUpdateCommand="MyGroupsRadGrid_UpdateCommand" OnItemDataBound="MyGroupsRadGrid_ItemDataBound" AllowSorting="true" AllowAutomaticUpdates="True" AllowAutomaticInserts="True" OnEditCommand="MyGroupsRadGrid_EditCommand" OnSelectedIndexChanged="MyGroupsRadGrid_SelectedIndexChanged1" AllowFilteringByColumn="false" OnCancelCommand="MyGroupsRadGrid_CancelCommand" MasterTableView-CommandItemDisplay="Top" OnInsertCommand="MyGroupsRadGrid_InsertCommand"> <ClientSettings AllowKeyboardNavigation="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true"> <Selecting AllowRowSelect="True" /> </ClientSettings> <MasterTableView DataKeyNames="BWGroupID"> <EditFormSettings EditFormType="Template"> <FormTemplate> <table id="Table1" class="marginleft50 bordercollapse width100percent" cellspacing="2" cellpadding="1" border="0" rules="none"> <tr> <td valign="top"> <table id="Table2" class="bordercollapse" cellspacing="2" cellpadding="1" border="0" rules="none"> <tr id="BWGroupIDRow" runat="server"> <td>Group ID </td> <td><asp:Label ID="BWGroupIDLabel" runat="server" Text='<%# Bind("BWGroupID") %>' /></td> </tr> <tr id="BWGroupNameRow" runat="server"> <td>Group Name </td> <td> <asp:TextBox ID="GroupNameTextBox" runat="server" Text='' /> <asp:RequiredFieldValidator ID="RequiredGroupName" runat="server" ControlToValidate="GroupNameTextBox" ForeColor="Red" Text="*A Group Name is Required." /> </td> </tr> <tr id="DisplayNameRow" runat="server"> <td>Group Owner </td> <td> <telerik:RadComboBox ID="OwnerRadComboBox" runat="server" OnClientSelectedIndexChanged="enableAddSelectionButton" Width="270px" DataTextField="Value" DataValueField="Key" ValidationExpression="[\w\s\.,_-]+$" AppendDataBoundItems="True" Filter="Contains"> <Items> <telerik:RadComboBoxItem runat="server" Text="" Value="-1" /> </Items> </telerik:RadComboBox> <br /> <asp:Label ID="OwnerError" runat="server" ForeColor="Red" Visible="true" /> </td> </tr> <tr id="IsActiveRow" runat="server"> <td>Status </td> <td><asp:Label ID="Label8" runat="server" Text='<%# Eval("IsActive").ToString() == "True" ? "Active" : "Inactive" %>' /></td> <td><asp:CheckBox ID="DeactivateCheckBox" runat="server" Text="Deactivate Group" /></td> </tr> <tr> <td align="center" colspan="2"> <br /> <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' /> <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" /> </td> </tr> </table> </td> <td valign="top"></td> </tr> </table> <br /> </FormTemplate> </EditFormSettings> <Columns> <telerik:GridEditCommandColumn ButtonType="LinkButton" EditText="Edit" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn> <telerik:GridBoundColumn UniqueName="BWGroupID" DataField="BWGroupID" HeaderText="Group ID" SortExpression="BWGroupID" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" /> <telerik:GridBoundColumn DataField="BWGroupName" HeaderText="Group Name" ReadOnly="true" SortExpression="BWGroupName" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" /> <telerik:GridBoundColumn DataField="ComputedBWGroupUserCount" HeaderText="Member Count" SortExpression="ComputedBWGroupUserCount" FilterControlWidth="50px" AutoPostBackOnFilter="true" /> </Columns> </MasterTableView></telerik:RadGrid>Code behind:
protected void MyGroupsRadGrid_InsertCommand(object sender, GridCommandEventArgs e){ int parseOwnerId = -1; try { GridEditableItem editedItem = (GridEditableItem)e.Item; if (e.CommandName == "PerformInsert") { #region Get controls TextBox GroupNameTextBox = new TextBox(); GroupNameTextBox = (TextBox)e.Item.FindControl("GroupNameTextBox"); RadComboBox OwnerRadComboBox = (RadComboBox)editedItem.FindControl("OwnerRadComboBox"); int.TryParse(OwnerRadComboBox.SelectedValue, out parseOwnerId); #endregion BWGroup newGroup = new BWGroup(); newGroup.BWUserIDCreated = BWSessionHandler.BWID; BWUser selectedOwner = new BWUser(); if (parseOwnerId > 0) { selectedOwner = this._bwContext.BWUsers.Where(x => x.BWUserID == parseOwnerId).FirstOrDefault(); if (selectedOwner.BWUserID != null && selectedOwner.BWUserID > 0) newGroup.BWUserIDOwner = selectedOwner.BWUserID; } newGroup.BWUserIDUpdated = BWSessionHandler.BWID; newGroup.BWGroupName = GroupNameTextBox.Text; newGroup.IsActive = true; newGroup.SystemGenerated = false; newGroup.RowInsertDateTime = DateTime.Now; this._bwContext.BWGroups.InsertOnSubmit(newGroup); this._bwContext.SubmitChanges(); ViewState["SelectedGroupId"] = newGroup.BWGroupID; MyGroupsRadGrid.MasterTableView.Items[0].Edit = false; MyGroupsRadGrid.MasterTableView.ClearEditItems(); MyGroupsRadGrid.Rebind(); GroupMemberRadGrid.MasterTableView.IsItemInserted = false; GroupMemberRadGrid.Rebind(); AssignNameOfGroup(newGroup.BWGroupName); GroupMemberRadGrid.Enabled = true; foreach (GridDataItem row in MyGroupsRadGrid.MasterTableView.Items) { if (row.GetDataKeyValue("BWGroupID").ToString() == ViewState["SelectedGroupId"].ToString()) { row.Selected = true; break; } } e.Canceled = true; } } catch (Exception ex) { this.ThrowError(ex, parseOwnerId); }}