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

Form Template Edit Form: Specified cast is not valid.

5 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fred Mare
Top achievements
Rank 1
Fred Mare asked on 18 Jun 2010, 10:20 AM
Hi

I am struggling to use a checkbox in a template edit form. When I click on the Add New Record button a Specified cast is not valid error is returned. I have implemented the following code that I found on another thread but the error still occurs. I am using an object datasource and binding. Here is the code that I am using.
Markup

 <asp:CheckBox ID="chkControlled" runat="server" Checked'<%# Bind( "controlled") %>'></asp:CheckBox> 

C# code

protected void radGridAssetCategories_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked  
            {  
                GridEditCommandColumn editColumn = (GridEditCommandColumn)radGridAssetCategories.MasterTableView.GetColumn("EditCommandColumn");  
                editColumn.Visible = false;  
                e.Canceled = true;  
                //Prepare an IDictionary with the predefined values    
                System.Collections.Specialized.ListDictionary newnewValues = new System.Collections.Specialized.ListDictionary();  
                //set initial checked state for the checkbox on init insert    
                newValues["controlled"] = false;   
                 
                //Insert the item and rebind    
                e.Item.OwnerTableView.InsertItem(newValues);  
                                 
 
            }  
            else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)  
            {  
                e.Canceled = true;  
            }  
            else  
            {  
                GridEditCommandColumn editColumn = (GridEditCommandColumn)radGridAssetCategories.MasterTableView.GetColumn("EditCommandColumn");  
                if (!editColumn.Visible)  
                    editColumn.Visible = true;  
            }  
        }  
 

Thank you
Fred

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Jun 2010, 12:44 PM
Hello Fred Mare,

You can try the code below and see whether this eliminates the error.

ASPX:
 
 <EditItemTemplate> 
    <asp:CheckBox ID="chkControlled" runat="server" Checked='<%# ((Container is GridEditFormInsertItem) ? false : Eval("controlled"))  %>'
    </asp:CheckBox> 
 </EditItemTemplate> 

Thanks,
Princy.



0
Fred Mare
Top achievements
Rank 1
answered on 18 Jun 2010, 02:44 PM
Dear Princy

Thank you for the quick reponse. It eliminates the error, but I need it to be binded as the object datasource is managing the insert to the database. The eval cause the value to be false all the time.

Fred
0
Pavlina
Telerik team
answered on 24 Jun 2010, 08:44 AM
Hello Fred,

In order to bind the controls inside the template, you must use a data binding expression on the appropriate control property. The example in the GridTemplateColumn section of this help article shows how to do this for a CheckBox column. Give it  a try and let me know if the problem still persists.

Sincerely yours,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Fred Mare
Top achievements
Rank 1
answered on 25 Jun 2010, 12:50 PM
Dear Pavlina

Thank you for your response.

I have used the Grid / Form Template Edit Form demo for my grid layout. I also am using binding for my controls inside the form template but it does not work for checkboxes or datepickers when you try to insert a new record. I am using a Formtemplate and not GridTemplateColumns. 

Thank you 
Fred
 
0
Pavlina
Telerik team
answered on 30 Jun 2010, 03:42 PM
Hi Fred,

You get this due to the fact that hasValue is null and Bind() expression will try to assign this to Checked property. To bind the Checked attribute value for your checkbox instance you need to specify default value (true/false) for hasValue. Thus you should be able to avoid the specified cast is not valid error.

Additionally, you can examine this forum thread:
http://www.telerik.com/community/forums/aspnet-ajax/grid/problem-displaying-bool-value-in-formtemplate-type-edit-form-insert-mode.aspx

All the best,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Fred Mare
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Fred Mare
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or