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

GridEditableItem

2 Answers 305 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Santosh Pradhan
Top achievements
Rank 1
Santosh Pradhan asked on 12 Apr 2010, 07:46 PM

I am using the FormTemplate to insert/update records from the grid. For most of the part the code is working fine. When I click the update button on the row in the grid, data is populated fine in the FormTemplate with the "Eval" command. The button for Insert/Update is also functioning well. 

On the Insert/Update button I am setting properties like following -
Text='<%# IIf (TypeOf Container is GridEditFormInsertItem, "Insert", "Update") %>'  CommandName='<%# IIf (TypeOf Container is GridEditFormInsertItem, "PerformInsert", "PerformUpdate") %>'

 

In the code behind I am calling stored procedures to insert or update:

Protected Sub RadGridAssgn_ItemCommand( .........

     ..

    If e.CommandName = RadGrid.PerformInsertCommandName Then

        'Insert new item

        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
        Dim aH as String = CType(editableItem.FindControl("txtNotes"), TextBox).Text
        ...
        ...
        'Call Insert StoredProcedure and Insert Item
       ...
       'Rebind and Refresh Grid
    ElseIf e.CommandName = "PerformUpdate" Then
      'Update Item
        Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
        Dim aH as String = CType(editableItem.FindControl("txtNotes"), TextBox).Text
        ...
        ...
        'Call Update StoredProcedure and Insert Item
       ...
       'Rebind and Refresh Grid
    End If

Insert part works fine. But I am having problem getting the update part working. The simple error I am getting at the following line:
        Dim aH as String = CType(editableItem.FindControl("txtNotes"), TextBox).Text

The same code works fine in the INSERT mode. In Update mode FindControl does not find the "txtNotes" text box and returns Null - I get an error "Object reference not set to an instance of an Object."

Any help is greatly appreciated!

Thanks!

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 13 Apr 2010, 06:13 AM
Hello Santhosh,

In order to find the TextBox in Update mode you should create an instance of GridEditFormItem instead of GridEditableItem. Hope the following code will help you.

If e.CommandName = "PerformUpdate" Then 
        Dim edititem As GridEditFormItem = DirectCast(TryCast(e.Item, GridDataItem).EditFormItem, GridEditFormItem) 
        Dim aH As String = DirectCast(edititem.FindControl("txtNotes"), TextBox).Text 
End If 

Regards,
Shinu.



0
Santosh Pradhan
Top achievements
Rank 1
answered on 13 Apr 2010, 06:30 PM
Thank you Shinu for the quick reply.

It worked fine. Now there is only one small issue. After I perform Insert, the grid gets back to the normal view (not in edit mode, FormTemplate is removed) but after Update, the grid stays in the Edit mode (FormTemplate stays) and I need to click the "Cancel" button to remove the FormTemplate. How can I remove the FormTemplate after update is performed?

Thanks,
Santosh
Tags
Grid
Asked by
Santosh Pradhan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Santosh Pradhan
Top achievements
Rank 1
Share this question
or