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

Exception when handling ItemCreated in CompositeControl

4 Answers 137 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RKemp
Top achievements
Rank 1
RKemp asked on 13 Jan 2009, 04:12 PM
I am working on a project that would benefit from adding a RadGrid to a CompositeControl.  The grid works as expected until I attempt to handle the ItemCreated event which results in the following exception:  Unable to cast object of type 'Telerik.Web.UI.GridItemEventHandler' to type 'Telerik.Web.UI.GridItemEventHandler'.

I have submitted a Telerik support ticket requesting assistance with this exception and to see if there is a different way of doing what I need. Unfortunatly after several exchanges with support I have found myself stuck in a "it works for me" loop.

Is there anyone in the community who can see an issue with the way I have coded this CompositeControl or is there a better/different way to create a CompositeControl containing a RadGrid that handles events that I can test?

Thank you!

CustomControls.dll (The CompositeControl with RadGrid)
using System.ComponentModel; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
namespace Company.CustomControls 
    [ToolboxData("<{0}:CustomGrid runat=server></{0}:CustomGrid>")] 
    public class CustomGrid : CompositeControl, INamingContainer 
    { 
        private RadGrid _MainGridView; 
 
        public CustomGrid() { 
            _MainGridView = new RadGrid(); 
        } 
 
        public object DataSource { 
            get { return _MainGridView.DataSource; } 
            set { _MainGridView.DataSource = value; } 
        } 
 
        protected override void CreateChildControls() { 
            Controls.Add(new LiteralControl("Custom Grid:<br />")); 
            _MainGridView.AutoGenerateColumns = true
            Controls.Add(_MainGridView); 
        } 
 
        public event GridItemEventHandler ItemCreated { 
            add { _MainGridView.ItemCreated += value; } 
            remove { _MainGridView.ItemCreated -= value; } 
        } 
    } 

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<%@ Register assembly="CustomControls" namespace="Company.CustomControls" tagprefix="cc1" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"><title>CustomGrid Test</title></head
<body> 
    <form id="form1" runat="server"
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
        <cc1:CustomGrid runat="server" ID="mainCustomGrid" onitemcreated="mainCustomGrid_ItemCreated"></cc1:CustomGrid> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs
using System; 
using System.Collections.Generic; 
 
public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        List<TestData> GridData = new List<TestData>(); 
        for (int i = 1000; i < 1025; i++) 
            GridData.Add(new TestData { Name = "Name " + i.ToString(), Phone = "555-555-" + i.ToString() }); 
        mainCustomGrid.DataSource = GridData; 
    } 
 
    protected void mainCustomGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) { 
        // Do work here... 
    } 
 
public class TestData 
    public string Name { getset; } 
    public string Phone { getset; } 

Exception
[InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.GridItemEventHandler' to type 'Telerik.Web.UI.GridItemEventHandler'.]
   Telerik.Web.UI.RadGrid.OnItemCreated(GridItemEventArgs e) +110
   Telerik.Web.UI.RadGrid.CallOnItemCreated(GridItemEventArgs e) +39
   Telerik.Web.UI.GridItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) +986
   Telerik.Web.UI.GridTableView.CreateHeaderItem(Boolean useDataSource, GridColumn[] copiedColumnSet, GridTHead thead) +126
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +980
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +802
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   Telerik.Web.UI.GridTableView.PerformSelect() +28
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   Telerik.Web.UI.GridTableView.DataBind() +368
   Telerik.Web.UI.RadGrid.DataBind() +165
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +3855
   Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +177
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.AddedControl(Control control, Int32 index) +265
   System.Web.UI.ControlCollection.Add(Control child) +80
   Company.CustomControls.CustomGrid.CreateChildControls() in C:\Data\CompositeControlGrid\TelerikTesting\CustomControls\CustomGrid.cs:28
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +44
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +84




4 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 13 Jan 2009, 05:19 PM
Hi RKemp,

Can you please give us more detail information about how the telerik's assemblies are referenced in the project in question?

Regards,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
RKemp
Top achievements
Rank 1
answered on 13 Jan 2009, 06:20 PM
The solution is made up of an ASP.NET Web Application and a class library which has the CompositeControl.  Telerik.Web.UI.dll 2008.3.1125.20 is referenced in both projects.  The test solution I created to assist with troubleshooting and recommendations is attached to ticket 183397.
0
Accepted
Rosen
Telerik team
answered on 14 Jan 2009, 05:23 PM
Hello RKemp,

I'm afraid that I was unable to recreated the behavior in question. Can you please check how the attached solution behaves on your machine?

Also can you please specify your  machine's .Net framework's version?

Greetings,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
RKemp
Top achievements
Rank 1
answered on 16 Jan 2009, 10:31 PM
The solution you attached worked as expected.
Tags
Grid
Asked by
RKemp
Top achievements
Rank 1
Answers by
Rosen
Telerik team
RKemp
Top achievements
Rank 1
Share this question
or