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

Unable to cast object of type 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'

17 Answers 938 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hariharan
Top achievements
Rank 1
Hariharan asked on 11 Feb 2009, 05:17 AM
Hi,
 I am getting the following error when I am performing insert using "PerformInsert" from CommandItemTemplate.
Unable to cast object of type 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'
ASPX Page : Grid Definition
<telerik:RadGrid ID="RadGrid1" Skin="Sunset" GridLines="None" runat="server" 
                 Width="97%" AllowPaging="True" AutoGenerateColumns="False" AllowMultiRowEdit="True" 
                OnNeedDataSource="RadGrid1_NeedDataSource"   
                AllowFilteringByColumn="True"    
                AllowSorting="True" PageSize="5" AllowMultiRowSelection="True"   
                ongridexporting="RadGrid1_GridExporting" OnItemCreated="RadGrid1_ItemCreated" 
                OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound">  
 
            <HeaderContextMenu Skin="Inox" EnableTheming="True">  
            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
            </HeaderContextMenu> 
 
                <PagerStyle Mode="NextPrevNumericAndAdvanced" /> 
                <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="DespatchNum" GridLines="None"   
                     AutoGenerateColumns="False" EditMode="EditForms">  
                    <Columns>                 
ASPX : Command Item Template Definition
 <CommandItemTemplate> 
                    <div style="padding: 0 5px;">  
                        Custom command item template&nbsp;&nbsp;&nbsp;&nbsp;  
                        <asp:LinkButton ID="btnEditSelected" runat="server" CommandName="EditSelected" Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'>  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Edit.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <asp:LinkButton ID="btnUpdateEdited" runat="server" CommandName="UpdateAll" Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'>  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Update.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <asp:LinkButton ID="btnCancel" runat="server" CommandName="CancelAll" Visible='<%# RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted %>' CausesValidation="False">  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Cancel.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <asp:LinkButton ID="btnAddNew" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'>  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/AddRecord.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <%--<asp:LinkButton ID="btnInsert" runat="server" CommandName="PerformInsert" Text="Insert" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'>  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Update.gif" /></asp:LinkButton>&nbsp;&nbsp;--%> 
                          
                        <asp:LinkButton ID="btnInsert" runat="server" CommandName="PerformInsert" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'>  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Update.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <asp:LinkButton ID="btnDelete" OnClientClick="javascript:return confirm('Delete all selected customers?')" runat="server" CommandName="DeleteAll">  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Delete.gif" /></asp:LinkButton>&nbsp;&nbsp;  
                          
                        <asp:LinkButton ID="btnRefresh" runat="server" CommandName="RebindGrid">  
                        <img style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Sunset/Refresh.gif" /></asp:LinkButton> 
                          
                        <asp:LinkButton ID="btnExportExcel" runat="server" CommandName="ExportToExcel">  
                        <img style="border:0px;vertical-align:middle;" alt="Export To Excel" src="" /></asp:LinkButton> 
                    </div> 
                </CommandItemTemplate> 

C# : Insert functionality inplemented in Item Command Event
 protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
        {  
            if (e.CommandName == "PerformInsert")  
            {  
                    //GridDataInsertItem insertedItem = e.Item as GridDataInsertItem;  
                GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
                  
                  
                //GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;  
 
                string Status = (insertedItem["DespatchStatus"].Controls[0] as TextBox).Text;  
                string RegionCode = (insertedItem["RegionCode"].FindControl("txtRegionCode"as TextBox).Text;  
                string LabCode = (insertedItem["LabCode"].Controls[0] as TextBox).Text;  
                string SampleCode = (insertedItem["SampleCode"].Controls[0] as RadComboBox).SelectedValue.ToString();  
            }  
        } 

I am getting error in line 6:
"GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;"

I have implemented Update and Delete methods similarly from Command Item Template, they seem to work fine. Just Insert is giving me this particular error.
But if i use the "Insert" button that comes by default in edit form, it is working fine.

I hope this information is sufficient. I you need further information please do let me know.

P.S : I have seen a few other suggestions in the forum saying to remove 
AllowAutomaticUpdates="true", AllowAutomaticInserts="true". I have removed all of them.

17 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 13 Feb 2009, 04:35 PM
Hello Hariharan,

As the exception message indicates, the e.Item instance (when clicking the button which resides in the command item template) is of type GridCommandItem, not GridEditFormInsertItem. I suggest you modify your code as follows to avoid the exception generated inside the InsertCommand handler:

protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)     
2         {     
3             if (e.CommandName == "PerformInsert")     
4             {     
5                     //GridDataInsertItem insertedItem = e.Item as GridDataInsertItem;     
6                 GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();     
7                      
8                      
9                 //GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;     
10     
11                 string Status = (insertedItem["DespatchStatus"].Controls[0] as TextBox).Text;     
12                 string RegionCode = (insertedItem["RegionCode"].FindControl("txtRegionCode"as TextBox).Text;     
13                 string LabCode = (insertedItem["LabCode"].Controls[0] as TextBox).Text;     
14                 string SampleCode = (insertedItem["SampleCode"].Controls[0] as RadComboBox).SelectedValue.ToString();     
15             }     
16         }    
 

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Hugo Furth
Top achievements
Rank 1
answered on 31 Oct 2009, 04:11 AM
I came across this thread and its very similar to my problem. I'm just trying to understand the eventargs in RadGrid.

So 2 questions:

1) If I take a regular old radgrid, drop it and hook everything up, the RadGrid1_InsertCommand   e.Item will be of type GridEditFormInsertItem which is easy enough to handle. However, if I use a command item template like this:

                <CommandItemTemplate>
                    <asp:Button ID="btnInsert" runat="server" CommandName="InitInsert" Font-Size="Smaller"
                        Height="19px" Text="Add Game" />
                    <asp:Button ID="btnPerformInsert" runat="server" CommandName="PerformInsert" Font-Size="Smaller"
                        Height="19px" Text="Complete" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'/>
                </CommandItemTemplate>

I then get e.Item of type GridCommandItem in the RadGrid1_InsertCommand. Why do I get a different argument?

2) So then I put my grid fields in an Item Template as as labels and hook them up. They display fine. But in the Properties of the grid, it is listed as RadGrid1.MasterTableView.ItemTemplate.Label1. How do I get a reference to it in the code-behind (specifically in the ItemCommand or Insert/Update/DeleteCommand?

TIA,
Hugo
0
Shinu
Top achievements
Rank 2
answered on 02 Nov 2009, 07:25 AM
Hi,

You can access the control in the ItemTemplate of the MAsterTableView using the following code snippet:

C#
protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            LinkButton btn = (LinkButton)dataItem.FindControl("LinkButton1"); 
            btn.BackColor = System.Drawing.Color.Red; 
        }  
    } 
 


Thanks,
Shinu
0
Hugo Furth
Top achievements
Rank 1
answered on 03 Nov 2009, 01:29 AM
Thanks for the quick reply but your code snippet doesn't really get me what I need  (but it is useful for some other functionality I need to implement!)

Let me try to explain the problem better:

I have MasterTableView templates for CommandItem (CommandItemTemplate), Item (ItemTemplate), and EditItem (EditItem Template)

The code for the two CommandItem buttons is above in my initial post. The first one fires the "InitInsert". The input is then done in-place using fields defined in the EditItemTemplate. When the user is done, he hits a button which fires "PerformInsert".

That immediately sends me to the RadGrid1_InsertCommand method with the e.item having type GridCommandItem. I don't seem to be able to get a reference to the objects in the edit template. The snippet you sent (for code to be executed in ItemDataBound method) is able to allow inspection of the existing items in the grid (specifically in the Item Template fields.)

But what I'm looking for is the fields to allow (for example) an insert. Most of the examples I've seen utilize a form which is not part of the grid (as in, for example, the original post which started this thread.) But I'm trying to use the inplace style with EditItem template fields. Any help towards helping me understand this is appreciated.

Hugo
0
Hugo Furth
Top achievements
Rank 1
answered on 04 Nov 2009, 03:06 AM
Just a follow up....

I'm starting to worry that I've described won't work and here's why:

Utilizing a Command Item template and an EditItem Template I have tried to trace thru all the events that occur when I hit the buttons to fire InitInsert and PerformInsert. (ref: the aspx code declaring the buttons in my original post above.)

The only event that looks like it holds anything resembling the fields I need is in the ItemDataBound method which is one of the events that fires subsequent to InitInsert. In RadGrid1_ItemDataBound I managed to do this:

    GridDataInsertItem d;  // scoped in page class 
    TextBox t;             // scoped in page class
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
        if (e.Item is GridDataInsertItem) 
            { 
            d = (GridDataInsertItem)e.Item; 
            t = (TextBox) d.FindControl("TextBox1"); 
            } 


this gives me a reference to TextBox1 which is my EditItem Template. At that point there is not yet any data in the field so, of course, it shows empty. So then I enter the data fields. But when I hit the button to fire PerformInsert and the RadGrid1_InsertCommand method is invoked, the BIG problem is that the reference to t is no longer valid!

So my question is this: Is there a place the grid component where I can access the newly entered data? If so, I'd sure like to know where!!!

Thanks again,
Hugo

PS - Is this whole question better answered as a support ticket?

0
Sebastian
Telerik team
answered on 04 Nov 2009, 02:12 PM
Hi Hugo,

You can get reference to the insertion form when triggering PerformInsert command from the command item using the MasterTableView.GetInsertItem() method from the server API of RadGrid. This method should return item of type GridEditFormInsertItem for which you can invoke the FindControl(textBoxId) method to locate the textbox and extract its value (inside the InsertCommand or ItemCommand server handler of the grid).

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Furth
Top achievements
Rank 1
answered on 11 Nov 2009, 12:16 AM
Sebastian -

Thanks for your answer, you pointed me in the right direction. However, just for anyone reading this thread, the code that worked is as follows:
                 GridDataInsertItem ii; 
                 ii = (GridDataInsertItem)RadGrid1.MasterTableView.GetInsertItem(); 
                 TextBox tb; 
                 tb = (TextBox)ii.FindControl("tbAway"); 

because (I believe) I am using an in-place editing rather than an edit form (as in your example.)

But this whole approach brought me to another another issue. The code above works fine if I have a Item template AND an EditItem template. Here's my Item emplate.
                <ItemTemplate> 
                    <asp:Label ID="lblAway" runat="server" Text='<%# Eval("AwayTeam") %>' Width="85px"></asp:Label> 
                    <asp:Label ID="lblHome" runat="server" Text='<%# Eval("HomeTeam") %>' Width="85px"></asp:Label> 
                    <asp:Label ID="lblGameSked" runat="server" Text='<%# Eval("SkedDateTime") %>' Width="85px"></asp:Label> 
                </ItemTemplate> 

However, if I REMOVE the item template but leave the EditItem template, the FindControl method (executed from either ItemCommand or InsertCommand) does not work (returns a null.) Can you tell me why this should be? Am I required to use an Item template whenever I have and EditItem template?

I ask because I have a number of grids I need to create and I'm trying to understand the behavior before I start making all the grids. I would sometimes like to allow the default item display rather than creating an Item template for every grid that needs an inplace EditItem template. But I guess I just need to know if I can/can't get reference my insert items without having an Item template to go along with my EditItem template.

Thx,
Hugo


0
Sebastian
Telerik team
answered on 11 Nov 2009, 12:54 PM
Hello Hugo,

This is indeed strange behavior and I suppose it might be related to the inplace editing you use. Does the issue vanishes when you use built-in edit forms mode? Most probably you need to have even empty item template definition in order to synchronize the cells updated for the edited items.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Furth
Top achievements
Rank 1
answered on 11 Nov 2009, 03:17 PM
Sebastian -

I will try using an edit form in the next couple of days and post my finding here. I don't think I can use an empty Item template because use of such would keep my (non-input) items from displaying. Isn't that right?

Hugo
0
Sebastian
Telerik team
answered on 11 Nov 2009, 03:22 PM
Hello Hugo,

Having empty ItemTemplate compared to no ItemTemplate at all should not make visual difference. Let me know if I am missing something from your logic.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Furth
Top achievements
Rank 1
answered on 11 Nov 2009, 03:44 PM
Hi Again Sebastian -

Maybe I'm missing something but here's how its been working for me. (Just tested it to double-check.)

If I have NO item template, then radgrid displays my grid data items as they've been specified in the radgrid Property Builder (default behavior, I presume.) If I have my own item template, then it uses that. So, for example, if I have 5 fields but I only put 3 of them in the item template, then I only see those 3. If I have no fields specified in an item template like this:
                <ItemTemplate> 
                    &nbsp; 
                </ItemTemplate> 
I get a blank line where each item would go. If I remove the blank line, I get a completely empty grid.

Is this unexpected behavior?

Regards,
Hugo
0
Sebastian
Telerik team
answered on 11 Nov 2009, 03:55 PM
Hello Hugo,

Your assumption is correct. However, my idea was to define an empty item template like this:

<ItemTemplate> 
</ItemTemplate> 

This should produce the same result as when not specifying item template at all.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Furth
Top achievements
Rank 1
answered on 11 Nov 2009, 04:05 PM
Sebastian -

That doesn't seem to work. It looks like just the presence of the <ItemTemplate> tag is enough to suppress/override the default item display.

Hugo
0
Sebastian
Telerik team
answered on 16 Nov 2009, 04:41 PM
Hello Hugo,

I tried to reproduce the problems in the sample project enclosed to this message (using the latest version 2009.3.1103 of RadControls for ASP.NET AJAX) but without success. Note that there are four identical couples of template columns in the grid  - one set without ItemTemplates and one with empty item template.

The FindControl method located the RadTextBox instances as expected on update/insert operation and inplace editing. Note that the borders of the cells which do not have item template defined will be missing under IE due to issue with borders for empty cells discussed in this forum thread.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Furth
Top achievements
Rank 1
answered on 16 Nov 2009, 07:15 PM
Thanks for this. I will check it out this week and compare with my code to try and zero in on the problem.

Thx,
Hugo
0
Hugo Furth
Top achievements
Rank 1
answered on 17 Nov 2009, 09:09 PM
Sebastian -

Thanks for putting together the example. Unfortunately, I was not able to open/run it. I am using Q2 and your example is Q3. I couldn't quickly figure out how to correct the config issue so I put my own example together. It uses the Northwind table employees.

As you will (hopefully) see, the addition of an edit item template without an item template causes the GetInsertItem() to malfunction. I have included my item template as a .txt file. You can add it back to the markup and then everything seems to work.

Also note another odd behavior: Without the item template, I get a nice calendar popup when inserting. Once I put the item template back in, the inplace editing becomes a textbox input. I find this pretty strange (though, I suppose, there could be a relationship.)

I have submitted a ticket on this so that I could send my example code.

Thanks again for your time and effort,
Hugo
0
Yavor
Telerik team
answered on 18 Nov 2009, 11:49 AM
Hi Hugo,

Since you sent the relevant file(s) in a separate ticket on the matter, in order to avoid duplicate posts, we will continue our communication there. The ticket in question will be addressed shortly.

Greetings,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Hariharan
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Hugo Furth
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Yavor
Telerik team
Share this question
or