This is a migrated thread and some comments may be shown as answers.

Insert item is available only when grid is in insert mode

3 Answers 331 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott Smith
Top achievements
Rank 1
Scott Smith asked on 04 Feb 2010, 08:22 PM
I'm having a problem with using a popup edit form with a RadGrid.

We are in a completely disconnected environment - all data is retrieved and updated through a WCF web service and in general is returned as LLBLGen entities/EntityCollection(s).  Nearly everything about this setup works (initial grid display, viewing the edit form in read-only, updating the database, etc.).  All database functionality is working great - the code-behind commands fire fine and the data in the database is inserted/updated.  The grid refreshes fine after inserts/updates as well, however, when performing an insert the popup editor stays on the screen and I get a javascript error:

"Insert item is available only when grid is in insert mode". 

When I run in debug mode, it's not throwing the error anywhere in code-behind, but somewhere in all the auto-generated ajax javascript, so I can't tell what exactly is causing the error.

ASPX:

<telerik:RadGrid ID="RadGrid1" runat="server"  
    AutoGenerateColumns="false" 
    AllowPaging="true" 
    AllowAutomaticInserts="false" 
    AllowAutomaticDeletes="false" 
    AllowAutomaticUpdates="false" 
    OnItemDataBound="RadGrid1_ItemDataBound" 
    OnNeedDataSource="RadGrid1_NeedDataSource" 
    OnInsertCommand="RadGrid1_InsertCommand" 
    OnUpdateCommand="RadGrid1_UpdateCommand" 
    OnPreRender="RadGrid1_PreRender" 
> 
    <MasterTableView  
        EditMode="PopUp"  
        CommandItemDisplay="Top"  
        DataKeyNames="NoteID"  
    > 
        <EditFormSettings InsertCaption="Add new Note" /> 
        <Columns> 
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton" HeaderStyle-Width="35px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" /> 
            <telerik:GridBoundColumn DataField="NoteID" UniqueName="NoteID" Visible="false" /> 
            <telerik:GridTemplateColumn HeaderText="!" UniqueName="Important" HeaderStyle-Font-Bold="true" HeaderStyle-Width="10px"
                <ItemTemplate> 
                    <asp:Label ID="lblImportant" runat="server" ForeColor="Red" Text='<%# (bool)Eval("Important")  ? "!" : "" %>' ToolTip="Note is marked as important" Font-Bold="true" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridBoundColumn HeaderText="Type" DataField="NoteType" UniqueName="NoteType" DataType="System.String" ItemStyle-Wrap="false" HeaderStyle-Width="110px" /> 
            <telerik:GridBoundColumn HeaderText="Recorded By" DataField="RecordedBy" UniqueName="RecordedBy" DataType="System.String" ReadOnly="true" HeaderStyle-Wrap="false" HeaderStyle-Width="120px" ItemStyle-Wrap="false" /> 
            <telerik:GridBoundColumn HeaderText="Date Created" DataField="ServerDate" UniqueName="ServerDate" DataType="System.DateTime" HeaderStyle-Wrap="false" HeaderStyle-Width="140px" ItemStyle-Wrap="true" /> 
            <telerik:GridBoundColumn HeaderText="Subject" DataField="SubjectSummary" UniqueName="SubjectSummary" DataType="System.String" /> 
        </Columns> 
        <EditFormSettings InsertCaption="Add new Note" CaptionFormatString="Edit Note" CaptionDataField="NoteID" EditFormType="Template"
            <FormTemplate> 
                <table cellpadding="4" cellspacing="0" width="100%" border="0"
                    <tr> 
                        <td style="width: 10px; white-space: nowrap; font-weight: bold;">Recorded By:</td> 
                        <td> 
                            <asp:Label ID="lblRecordedBy" runat="server" Text='<%# Bind("RecordedBy") %>' /> 
                        </td> 
                        <td style="font-weight: bold;">on</td> 
                        <td> 
                            <asp:Label ID="lblServerDate" runat="server" Text='<%# Bind("ServerDate") %>' /> 
                        </td> 
                        <td style="width: 10px; font-weight: bold;">Type:</td> 
                        <td style="width: 114px;"
                            <telerik:RadComboBox ID="cboType" runat="server" SkinID="RadCombo" Width="110px" /> 
                        </td> 
                    </tr> 
                </table> 
                <table cellpadding="4" cellspacing="0" width="100%" border="0"
                    <tr> 
                        <td style="width: 40px; font-weight: bold;">Subject:</td> 
                        <td colspan="3"
                            <telerik:RadTextBox ID="txtSubject" runat="server"  
                                SkinID="RadTextBoxDefault" 
                                Text='<%# DataBinder.Eval(Container.DataItem, "Subject") %>' 
                                Width="100%" 
                            /> 
                        </td> 
                        <td style="width: 40px; font-weight: bold;">Important?</td> 
                        <td style="width: 10px;"><asp:CheckBox ID="chkImportant" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? false : Eval("Important") %>' /></td
                    </tr> 
                    <tr> 
                        <td colspan="100%" valign="top" align="left"
                            <telerik:RadEditor ID="radEditor_Note" runat="server" 
                                 Width="100%" 
                                 Height="250px" 
                                 Content='<%# DataBinder.Eval(Container.DataItem, "Note") %>' 
                                 AutoResizeHeight="true" 
                                 EditModes="Design" 
                                 ToolbarMode="Default" 
                             > 
                                <Tools> 
                                    <telerik:EditorToolGroup> 
                                        <telerik:EditorTool Name="Bold" /> 
                                        <telerik:EditorTool Name="Italic" /> 
                                        <telerik:EditorTool Name="Underline" /> 
                                        <telerik:EditorSeparator /> 
                                        <telerik:EditorTool Name="JustifyLeft" /> 
                                        <telerik:EditorTool Name="JustifyCenter" /> 
                                        <telerik:EditorTool Name="JustifyRight" /> 
                                        <telerik:EditorSeparator /> 
                                        <telerik:EditorTool Name="AjaxSpellCheck" /> 
                                        <telerik:EditorSeparator /> 
                                        <telerik:EditorTool Name="ToggleScreenMode" /> 
                                    </telerik:EditorToolGroup> 
                                </Tools> 
                            </telerik:RadEditor> 
                        </td> 
                    </tr> 
                </table> 
                <table width="100%" style="margin-top: 10px;"
                    <tr> 
                        <td align="right" colspan="100%"
                            <asp:Button ID="btnUpdate" runat="server"  
                                Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' 
                                SkinID="btnSearch" 
                                Visible='<%# (AllowAdd || AllowEdit) ? true : false %>' 
                             /> 
                            &nbsp; 
                            <asp:Button ID="btnCancel" runat="server" 
                                Text='<%# (AllowAdd || AllowEdit) ? "Cancel" : "Close" %>'  
                                CausesValidation="False" 
                                CommandName="Cancel" 
                                SkinID="btnSearch" 
                            /> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td colspan="100%"
                        </td> 
                    </tr> 
                </table> 
                <asp:Label ID="lblNoteTypeID" runat="server" Text='<%# Bind("NoteTypeID") %>' Visible="false" /> 
            </FormTemplate> 
        </EditFormSettings> 
    </MasterTableView> 
    <ClientSettings> 
        <ClientEvents OnRowDblClick="RowDblClick" OnPopUpShowing="PopUpShowing" /> 
        <Scrolling AllowScroll="true" /> 
    </ClientSettings> 
</telerik:RadGrid> 

And the code behind:

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    // enable/disable command items based on security 
    foreach (GridCommandItem item in RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)) 
    { 
        if (!AllowAdd) 
        { 
            ((Button)item.FindControl("AddNewRecordButton")).Visible = false
            ((LinkButton)item.FindControl("InitInsertButton")).Visible = false
        } 
 
        ((LinkButton)item.FindControl("RebindGridButton")).Visible = false
        ((Button)item.FindControl("RefreshButton")).Visible = false
    } 
 
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    LoadData(); 
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    { 
        // get handles for the controls in the FormTemplate 
        GridEditableItem item = (GridEditableItem)e.Item; 
        Label lblNoteTypeID = item.FindControl("lblNoteTypeID"as Label; 
        Label lblRecordedBy = item.FindControl("lblRecordedBy"as Label; 
        Label lblServerDate = item.FindControl("lblServerDate"as Label; 
        RadComboBox cboType = item.FindControl("cboType"as RadComboBox; 
        RadTextBox txtSubject = item.FindControl("txtSubject"as RadTextBox; 
        CheckBox chkImportant = item.FindControl("chkImportant"as CheckBox; 
        RadEditor radEditor_Note = item.FindControl("radEditor_Note"as RadEditor; 
        Button btnUpdate = item.FindControl("btnUpdate"as Button; 
        Button btnCancel = item.FindControl("btnCancel"as Button; 
        string noteTypeID = lblNoteTypeID.Text; 
 
        // set the combo 
        switch (ForeignKeyType) 
        { 
            case ForeignKeyTypes.Branch: 
                bindComboToEnum(cboType, ListCategory.BranchNoteType); 
                break
 
            case ForeignKeyTypes.BranchProspect: 
                bindComboToEnum(cboType, ListCategory.BranchProspectNoteType); 
                break
 
            case ForeignKeyTypes.Recruit: 
                bindComboToEnum(cboType, ListCategory.RecruitNoteType); 
                break
 
            defaultbreak
        } 
 
        // Edit versus Insert logic 
        if (e.Item.OwnerTableView.IsItemInserted) 
        { 
            // insert mode 
            lblRecordedBy.Text = CurrentUser.FullName; 
            lblServerDate.Text = DateTime.Now.ToString(); 
        } 
        else 
        { 
            // edit mode 
            cboType.SelectedIndex = cboType.Items.IndexOf(cboType.Items.FindItemByValue(noteTypeID)); 
            cboType.Enabled = AllowEdit; 
            txtSubject.ReadOnly = !AllowEdit; 
            chkImportant.Enabled = AllowEdit; 
            radEditor_Note.Enabled = AllowEdit; 
            btnUpdate.Visible = AllowEdit; 
            btnCancel.Text = "Close"
        } 
    } 
    else if (e.Item is GridHeaderItem) 
    { 
        GridHeaderItem headerItem = e.Item as GridHeaderItem; 
        headerItem["EditCommandColumn"].Text = AllowEdit ? "Edit" : "View"
    } 
 
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    GridEditableItem item; 
    NoteEntity note; 
    int newNoteTypeID; 
    string msg = string.Empty; 
 
    item = e.Item as GridEditableItem; 
    newNoteTypeID = (item.FindControl("cboType"as RadComboBox).SelectedValue.ToInt(); 
    note = ServiceProxy.getService<INoteService>().Fetch(item.GetDataKeyValue("NoteID").ToString().ToInt(), FetchTypes.Heavy); 
 
    // update the note 
    note.Note = (item.FindControl("radEditor_Note"as RadEditor).Content; 
    note.Important = (item.FindControl("chkImportant"as CheckBox).Checked; 
    note.Subject = (item.FindControl("txtSubject"as RadTextBox).Text; 
 
    // update the note type 
    switch (ForeignKeyType) 
    { 
        case ForeignKeyTypes.Branch: 
            if (note.BranchNote != null
                note.BranchNote[0].BranchNoteTypeID = newNoteTypeID; 
            break
 
        case ForeignKeyTypes.BranchProspect: 
            if (note.BranchProspectNote != null
                note.BranchProspectNote[0].BranchProspectNoteTypeID = newNoteTypeID; 
            break
 
        case ForeignKeyTypes.Recruit: 
            if (note.RecruitApplicationNote != null && note.RecruitApplicationNote.Count > 0) 
                note.RecruitApplicationNote[0].RecruitNoteTypeID = newNoteTypeID; 
            break
 
        defaultbreak
    } 
 
    // perform the save 
    note = ServiceProxy.getService<IEntityService>().EntitySave(note) as NoteEntity; 
    msg = note.StatusMessage; 
 
    if (msg.Length > 0) 
    { 
        lblStatusMessage.Text = msg; 
        lblStatusMessage.ForeColor = System.Drawing.Color.Red; 
        ErrorHandler.Handle(new Exception(msg), Request); 
    } 
 
    e.Item.OwnerTableView.ClearEditItems(); 
    LoadData(true); 
// RadGrid1_UpdateCommand 
 
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) 
    GridEditableItem item; 
    NoteEntity note; 
    int newNoteTypeID; 
    string msg = string.Empty; 
 
    item = e.Item.OwnerTableView.GetInsertItem() as GridEditableItem; 
    note = new NoteEntity(); 
 
    newNoteTypeID = (item.FindControl("cboType"as RadComboBox).SelectedValue.ToInt(); 
    note.Note = (item.FindControl("radEditor_Note"as RadEditor).Content; 
    note.Important = (item.FindControl("chkImportant"as CheckBox).Checked; 
    note.Subject = (item.FindControl("txtSubject"as RadTextBox).Text; 
    note.ClientDate = DateTime.Now; 
    note.RecordedBy = CurrentUser.FullName; 
 
    // add the note type 
    switch (ForeignKeyType) 
    { 
        case ForeignKeyTypes.Branch: 
            BranchNoteEntity bne = new BranchNoteEntity(ForeignKey, note.NoteID); 
            note.BranchNote.Add(bne); 
            break
 
        case ForeignKeyTypes.BranchProspect: 
            BranchProspectNoteEntity bpne = new BranchProspectNoteEntity(ForeignKey.ToInt(), note.NoteID); 
            note.BranchProspectNote.Add(bpne); 
            break
 
        case ForeignKeyTypes.Recruit: 
            RecruitApplicationNoteEntity rne = new RecruitApplicationNoteEntity(ForeignKey.ToInt(), note.NoteID); 
            note.RecruitApplicationNote.Add(rne); 
            break
 
        defaultbreak
    } 
 
    // perform the save 
    note = ServiceProxy.getService<IEntityService>().EntitySave(note) as NoteEntity; 
    msg = note.StatusMessage; 
 
    if (msg.Length > 0) 
    { 
        lblStatusMessage.Text = msg; 
        lblStatusMessage.ForeColor = System.Drawing.Color.Red; 
        ErrorHandler.Handle(new Exception(msg), Request); 
    } 
 
    e.Item.OwnerTableView.IsItemInserted = false
    LoadData(true); 
// RadGrid1_InsertCommand 
 
private void LoadData() 
    LoadData(false); 
private void LoadData(bool rebind) 
    DataSet dsSource = ServiceProxy.getService<INoteService>().FetchNotes(ForeignKeyType, ForeignKey, FetchTypes.Heavy); 
 
    if (dsSource.Tables.Count > 0) 
    { 
        DataColumn dc = new DataColumn("SubjectSummary", Type.GetType("System.String")); 
        dsSource.Tables[0].Columns.Add(dc); 
        foreach (DataRow dr in dsSource.Tables[0].Rows) 
        { 
            string summary = dr["Subject"].ToString().StripTags(); 
            dr["SubjectSummary"] = summary.Length < SummaryLength ? summary : string.Format("{0}...", summary.Left(SummaryLength - 3)); 
        } 
 
        RadGrid1.DataSource = dsSource; 
        if (rebind) 
            RadGrid1.DataBind(); 
    } 
    ErrorHandler.Handle(new ArgumentException(string.Format("No data was returned for the FetchNotes method:\n\nForeignKeyType: {0}\nForeignKey: {1}\n", ForeignKeyType, ForeignKey)), this.Request); 

I've tried all the suggestions from the forums that I could find, including doing the "e.Item.OwnerTableView.IsItemInserted = false;" command, doing a rebind after that, setting the AllowAutomaticInserts to false, etc.

Any ideas?

Thanks
-
Scott

3 Answers, 1 is accepted

Sort by
0
Scott Smith
Top achievements
Rank 1
answered on 05 Feb 2010, 02:54 PM
I believe I have found the fix to my issue.  In the InsertCommand handler, before I rebind the grid I added:

e.Canceled = true

That seems to have done the trick.

-
Scott


0
Yusra
Top achievements
Rank 1
answered on 27 Jan 2014, 09:56 AM
Thanks Scott your answer helps me too. 
0
Twinkle
Top achievements
Rank 1
answered on 27 Oct 2015, 12:13 PM

it works Scott.

thanks.

Tags
Grid
Asked by
Scott Smith
Top achievements
Rank 1
Answers by
Scott Smith
Top achievements
Rank 1
Yusra
Top achievements
Rank 1
Twinkle
Top achievements
Rank 1
Share this question
or