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

FindControl returns null when editing but not inserting

2 Answers 281 Views
Grid
This is a migrated thread and some comments may be shown as answers.
johnv
Top achievements
Rank 2
johnv asked on 26 May 2010, 10:10 PM
I'm trying to use the RadGrid to do in-place editing and I am having an issue.

When press the "+ Add" button, I can enter the fields and when I press save, everything is working fine.  When I try to edit the same record with the "Edit" button below it can't find any of the templated controls. (It's a template column here, but the same thing happens when I use GridEditCommandColumn)

This is the ASCX code:

    <telerik:RadGrid ID="gridNotes" AutoGenerateColumns="False" Skin="Web20" runat="server">  
        <MasterTableView AutoGenerateColumns="False" AllowAutomaticUpdates="false" AllowAutomaticInserts="false" CommandItemDisplay="Top" DataKeyNames="NoteID" 
            TableLayout="Fixed" EditMode="EditForms">  
            <Columns> 
                <telerik:GridBoundColumn UniqueName="ID" DataField="NoteID" Display="false">  
                </telerik:GridBoundColumn> 
                <telerik:GridDateTimeColumn HeaderText="Date" UniqueName="Date" DataField="Date" 
                    DataType="System.DateTime" HeaderStyle-Width="170px" EmptyDataText=" ">  
                </telerik:GridDateTimeColumn> 
                <telerik:GridBoundColumn HeaderText="Owner" UniqueName="Owner" DataField="Author" 
                    HeaderStyle-Width="180px" ColumnEditorID="ownerEditor">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn HeaderText="Notes" UniqueName="Body" HeaderStyle-Width="250px" DataField="Body">  
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn HeaderText="Attachments" UniqueName="Attachments" ItemStyle-Width="250px">  
                    <ItemTemplate> 
                        <table style="border:0px; height:10px;">  
                            <tr> 
                                <td valign="middle">  
                                    <asp:ImageButton ID="btnAddAttachments" ImageUrl="~/Images/ic_attachments_add.png" runat="server" /> 
                                </td> 
                                <td valign="middle" style="width:49%">  
                                    Add&nbsp;>>  
                                </td> 
                                <td valign="middle">  
                                    <asp:ImageButton ID="btnViewAttachments" ImageUrl="~/Images/ic_attachments_view.png" runat="server" /> 
                                </td> 
                                <td valign="middle" style="width:49%">  
                                    View&nbsp;>>  
                                </td> 
                            </tr> 
                        </table> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn HeaderText="Save" UniqueName="EditCommand" ItemStyle-Width="135px" ItemStyle-HorizontalAlign="Center">  
                    <ItemTemplate> 
                        <asp:Button ID="btnEdit" CssClass="miniButton" Width="60px" Text="Edit" CommandName="Edit" runat="server" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
            </Columns> 
            <EditFormSettings EditFormType="Template">  
                <FormTemplate> 
                    <table cellpadding="0" cellspacing="0" width="100%">  
                        <tr> 
                            <td style="width:170px; text-align:center;">  
                                <telerik:RadDatePicker ID="dateNote" runat="server"></telerik:RadDatePicker> 
                            </td> 
                            <td style="width:180px; text-align:center;">  
                                <asp:TextBox ID="txtOwner" Width="170px" Text='<%# Bind("Author") %>' runat="server"></asp:TextBox> 
                            </td> 
                            <td style="width:250px; text-align:center;">  
                                <asp:TextBox ID="txtBody" TextMode="MultiLine" Height="60px" Width="240px" Text='<%# Bind("Body") %>' Font-Names="Helvetica" runat="server"></asp:TextBox> 
                            </td> 
                            <td style="width:250px; text-align:center;">  
                                <telerik:RadAsyncUpload ID="attachmentNotes" Width="240px" Skin="Web20" runat="server"></telerik:RadAsyncUpload> 
                            </td> 
                            <td style="width:65px; text-align:center;">  
                                <asp:Button ID="btnSave" CssClass="miniButton" Width="60px" Text="Save" CommandName="Save" runat="server" /> 
                            </td> 
                            <td style="width:70px; text-align:center;">  
                                <asp:Button ID="btnCancel" CssClass="miniButton" Width="60px" Text="Cancel" CommandName="Cancel" runat="server" /> 
                            </td> 
                        </tr> 
                    </table> 
                </FormTemplate> 
            </EditFormSettings> 
        </MasterTableView> 
        <ClientSettings> 
        </ClientSettings> 
    </telerik:RadGrid> 
    <asp:HiddenField ID="txtNote" runat="server" /> 
 

This is in the code-behind.

 

void gridNotes_ItemCommand(object source, GridCommandEventArgs e)

 

{
    

switch (e.CommandName)

 

    {

 

        case RadGrid.InitInsertCommandName:

 

 

            if (gridNotes.EditItems.Count > 0)

 

                gridNotes.MasterTableView.ClearEditItems();

            NoteID = 0;

 

            break;

 

 

        case RadGrid.EditCommandName:

 

            _gridItem = (

GridEditableItem)e.Item;

 

            NoteID =

Convert.ToDouble(_gridItem.GetDataKeyValue("NoteID"));

 

            e.Item.OwnerTableView.IsItemInserted =

false;

 

 

            break;

 

 

        case "Save":

 

            e.Item.Edit =

false;

 

            _gridItem = (

GridEditableItem)e.Item;

 

            _presenter.SaveNote();

            gridNotes.MasterTableView.ClearEditItems();

 

            break;

 

 

        case "Cancel":

 

            gridNotes.MasterTableView.ClearEditItems();

 

            break;

 

 

        default:

 

            gridNotes.MasterTableView.ClearEditItems();

 

            break;

 

    }

}

In the SaveNote() method, the controls are accessed through properties that look like this:

((RadDatePicker)_gridItem.FindControl("dateNote")).SelectedDate;
((TextBox)_gridItem.FindControl("txtOwner")).Text;

 

 

etc.

I get "Object reference not set to an instance of an object.", because although _gridItem exists, the FindControl() returns null.

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 27 May 2010, 12:42 PM
Hello John,

I think the problem is because of using same CommandName for update and insert. If you are using Form Template, you have to use seperate CommandName to perform update and insert, based on the form opened. Then inside ItemCommand event check for the CommandName and write corresponding code.

You can also refer the following Demo which explains how to update/insert records manually using Form Template custom edit form.
Form Template Edit Form

Regards,
Shinu.




0
johnv
Top achievements
Rank 2
answered on 27 May 2010, 02:37 PM
That worked.  Thanks.  Just changed the aspx:

CommandName

 

='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'

 


And doubled up my switch statement to:

 

case "Insert":

 

 

case "Update":

 


After that, everything worked.
Tags
Grid
Asked by
johnv
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
johnv
Top achievements
Rank 2
Share this question
or