More in this section

Forums / Developing with Sitefinity / how to cast object of type 'Telerik.Cms.Data.CmsWebControl' to type 'System.Web.UI.Control'.

how to cast object of type 'Telerik.Cms.Data.CmsWebControl' to type 'System.Web.UI.Control'.

4 posts, 0 answered
  1. sarath
    sarath avatar
    24 posts
    Registered:
    24 Jan 2010
    09 Feb 2010
    Link to this post
    Hi,

    I am getting the below exception when i tried to cast to type 'Telerik.Cms.Data.CmsWebControl' to type 'System.Web.UI.Control'.

    Exception:
    {System.InvalidCastException: Unable to cast object of type 'Telerik.Cms.Data.CmsWebControl' to type 'System.Web.UI.Control'.

    The below is the code snippet iam trying to cast,

    public ICmsWebControl GetCMSControlName(string contentAreaname, string templateName)
    {
    // based on the control name returning the ICMSWebControl
    }

    protected void Page_Load(object sender, EventArgs e)
          {
    Telerik.Cms.ICmsWebControl webControl = GetCMSControlName("footer", "parentmaster")
                this.Controls.Add((Control)webControl);
    }

    Thanks,
    Sarath
     
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Feb 2010
    Link to this post
    Hi sarath,

    webControl seems to be  ICmsWebControl but you are trying to add it with type Control to the controls collection.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. sarath
    sarath avatar
    24 posts
    Registered:
    24 Jan 2010
    09 Feb 2010
    Link to this post

    Thank you for your reply. The problem iam facing here is I am reading all the controls from the template control programmatically, the controls could be GenericContent, Site Tabstrip, sitePanelBar , videolist etc...want to render these controls dynamically in the non cms pages.

    Thank you,
    Sarath
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    10 Feb 2010
    Link to this post
    Hi sarath,

    You should basically check if the control is .ascx or not. You can take a look at the bellow code snippet:
    CmsManager cmsManager = new CmsManager();
    IPageTemplate template = cmsManager.GetTemplate("Default");
    foreach(ICmsWebControl control in template.Controls)
    {
        string controlType = control.ControlType.ToString();
        bool isAscx = control.IsAscx;
        if (isAscx)
        {
            string[] str = controlType.Split('.');
            string path = str[1];
            path = path.Replace("_ascx", ".ascx");
            path = path.Replace('_', '/');
            path = string.Concat("~/", path);
            Control ctrl1 = Page.LoadControl(path);
            Page.Controls.Add(ctrl1);
        }
        else
        {
            Page.Controls.Add(control.LoadControl());
        }
    }


    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Register for webinar
4 posts, 0 answered