1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How do I use an embedded .ascx Dialog Template for a WebUITypeEditor?

How do I use an embedded .ascx Dialog Template for a WebUITypeEditor?

4 posts, 0 answered
  1. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    28 Jan 2010
    Link to this post
    In my WebUITypeEditor file I want to set the dialog template path to an embedded .ascx file within the same library. This is the property I am trying to set for the embedded path but it is not working:

            /// <summary>
            /// Gets or sets the path of the Dialog template
            /// </summary>
            public string DialogTemplatePath
            {
                get
                {
                    string value = (string)ViewState["DialogTemplatePath"];
                    return String.IsNullOrEmpty(value) ? "MyNamespace.Resources.ControlTemplates.Backend.FormsListItemEditor.ascx" : value;
                }
                set
                {
                    ViewState["DialogTemplatePath"] = value;
                }
            }

    But I get this error when I open up the dialog template from the admin:

    Exception Details: System.ArgumentException: The relative virtual path 'MyNamespace.Resources.ControlTemplates.Backend.FormsListItemEditor.ascx' is not allowed here.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 Jan 2010
    Link to this post
    Hi bemara57,

    You have to use WebSysTemplate and initialize the template.

    below is a sample code

    [WebSysTemplate(MySelector.MySelectorTemplateName, "MySelector_Desc", "/MySelector", false, "2009-01-31")]
     public string TemplatePath
     {
         get
         {
             return this.layoutTemplatePath;
         }
         set
         {
             this.layoutTemplatePath = value;
         }
     }
     
     
    protected virtual string LayoutTemplateName
       {
             get
             {
                  return MySelector.MySelectorTemplateName;
             }
       }
     
     
        public ITemplate Template
     {
         get
         {
             if (this.template == null)
                 this.template = ControlUtils.GetTemplate(this.TemplatePath, this.LayoutTemplateName, this.GetType(), this.GetType(), this.GetType(), this.ProviderName);
             return this.template;
         }
         set
         {
             this.template = value;
         }
     }
     
     
        private const string MySelectorTemplateName =
         "Telerik.Samples.Templates/Mytemplate.ascx";



    Sincerely yours,
    Ivan Dimitrov
    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.
  3. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    28 Jan 2010
    Link to this post

    Thanks, but I am getting this error now:

    Exception Details: System.FormatException: Invalid Property.

    This was dying on this line:
    this._dialogTemplate = ControlUtils.GetTemplate(this.DialogTemplatePath, this.DialogTemplateName, this.GetType(), this.GetType(), this.GetType(), (new FormsControlPanel()).ProviderName);

    Putting a breakpoint there showed that this.DialogTemplatePath was a blank string, so it's not getting filled in by the WebSysTemplate attribute.

    I tried with all periods and just one slash like you had in case that was not a typo for the path, but in that case I got this error:

    Exception Details: System.ArgumentException: Invalid resource name (MyNamespace.SomeProject.Forms.Resources.ControlTemplates.Backend/FormsListItemEditor.ascx) for assembly (MyNamespace.SomeProject.Forms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null) or empty template.

    I think these errors are happening because I'm not inheriting from SimpleControl like it was meant to for this attribute. But how do I get this functionality while still inheriting from WebUITypeEditor?

    using System;  
    using System.Collections;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Collections.Generic;  
    using Telerik.Cms.Web.UI;  
    using Telerik.Framework.Web;  
    using Telerik.Web.UI;  
    using System.ComponentModel;  
     
    namespace MyNamespace.SomeProject.Forms.WebControls.Admin  
    {  
        public class FormsListItemEditor : WebUITypeEditor<string>, IControlPropertyEditor  
        {  
            #region Private Members  
     
            private const string layoutTemplateName = "MyNamespace.SomeProject.Forms.Resources.ControlTemplates.Backend.FormsListItemEditor.ascx";  
            private string layoutTemplatePath = String.Empty;  
            private string _selectedForm;  
            private ITemplate _dialogTemplate;  
            private Container _dialogContainer;  
            private Dictionary<string, object> _dependentProps;  
     
            #endregion  
     
            #region Properties  
     
            /// <summary> 
            /// Gets or sets the Value property - Value property represents the property which TypeEditor  
            /// will edit of the parent control which fired the type editor.  
            /// </summary> 
            public override string Value  
            {  
                get  
                {  
                    return _selectedForm;  
                }  
                set  
                {  
                    _selectedForm = value;  
                }  
            }  
     
            /// <summary> 
            /// Gets or sets the path of the Dialog template  
            /// </summary> 
            [WebSysTemplate(FormsListItemEditor.layoutTemplateName, "FormsListItemEditor_Template_Desc", "/Forms", false, "2009-01-31")]  
            public string DialogTemplatePath  
            {  
                get { return this.layoutTemplatePath; }  
                set { this.layoutTemplatePath = value; }  
            }  
     
            /// <summary> 
            /// Gets or sets the name of the Dialog template  
            /// </summary> 
            public virtual string DialogTemplateName  
            {  
                get { return FormsListItemEditor.layoutTemplateName; }  
            }  
     
            /// <summary> 
            /// Gets or sets the Dialog template  
            /// </summary> 
            public ITemplate DialogTemplate  
            {  
                get  
                {  
                    if (this._dialogTemplate == null)  
                    {  
                        this._dialogTemplate = ControlUtils.GetTemplate(this.DialogTemplatePath, this.DialogTemplateName, this.GetType(), this.GetType(), this.GetType(), (new FormsControlPanel()).ProviderName);  
                    }  
     
                    return this._dialogTemplate;  
                }  
                set  
                {  
                    _dialogTemplate = value;  
                }  
            }  
     
            /// <summary> 
            /// Returns a dictionary with the dependent properties  
            /// </summary> 
            /// <remarks> 
            /// DependentProperties property allows us to modify additional properties  
            /// (not just the value of the property which fired TypeEditor) of the control which  
            /// has called TypeEditor.  
            /// </remarks>          
            public IDictionary<string, object> DependentProperties  
            {  
                get  
                {  
                    if (_dependentProps == null)  
                    {  
                        _dependentProps = new Dictionary<string, object>();  
                    }  
                    return _dependentProps;  
                }  
            }  
     
            #endregion  
     
            #region Default Templates  
     
            /// <summary> 
            /// Class which defines the template for this control, in case template has not been  
            /// defined through an external file or through inline declaration  
            /// </summary> 
            private class DefaultDialogTemplate : ITemplate  
            {  
                public void InstantiateIn(Control container)  
                {  
                    throw new Exception("Default template for FormListItemEditor has not been implemented.");  
                }  
            }  
     
            #endregion  
     
            #region Containers  
     
            /// <summary> 
            /// Class which provides easy, strongly-typed access to the child controls defined in a template  
            /// regardless of their position in the child control hierarchy  
            /// </summary> 
            private class Container : GenericContainer<FormsListItemEditor> 
            {  
                public Container(FormsListItemEditor owner) : base(owner)  
                {  
                }  
     
                public RadGrid FormsGrid  
                {  
                    get  
                    {  
                        if (formsGrid == null)  
                            formsGrid = FindRequiredControl<RadGrid>("formsGrid");  
                        return formsGrid;  
                    }  
                }  
     
                private RadGrid formsGrid;  
            }  
     
            #endregion  
     
            #region Methods  
     
            /// <summary> 
            /// Overriden. Cancels the rendering of a beginning HTML tag for the control.  
            /// </summary> 
            /// <param name="writer">The HtmlTextWriter object used to render the markup.</param> 
            public override void RenderBeginTag(HtmlTextWriter writer)  
            {  
                // Do not render begin tag  
            }  
     
            /// <summary> 
            /// Overriden. Cancels the rendering of an ending HTML tag for the control.  
            /// </summary> 
            /// <param name="writer">The HtmlTextWriter object used to render the markup.</param> 
            public override void RenderEndTag(HtmlTextWriter writer)  
            {  
                // Do not render end tag  
            }  
     
            /// <summary> 
            /// Overrides the base method and registers ControlDesignerBase control as one whose control state   
            /// must be persisted.  
            /// </summary> 
            /// <param name="e"></param> 
            protected override void OnInit(EventArgs e)  
            {  
                base.OnInit(e);  
                if (Page != null)  
                    Page.RegisterRequiresControlState(this);  
            }  
     
            /// <summary> 
            /// Restores control state information from a previous page request that was saved by the SaveControlState  
            /// method.  
            /// </summary> 
            /// <param name="savedState">Represents the control state to be restored.</param> 
            protected override void LoadControlState(object savedState)  
            {  
                if (savedState != null)  
                {  
                    object[] state = (object[])savedState;  
                    _selectedForm = (string)state[0];  
                }  
            }  
     
            /// <summary> 
            /// Saves server control state changes.  
            /// </summary> 
            /// <returns></returns>  
            protected override object SaveControlState()  
            {  
                return new object[] {    
                    _selectedForm  
                };  
            }  
     
            /// <summary> 
            /// Overriden. Called to populate the child control hierarchy. This is the main  
            /// method to render the control's markup, since it is a CompositeControl and contains  
            /// child controls.  
            /// </summary> 
            protected override void CreateChildControls()  
            {  
                // initialize the container and template  
                _dialogContainer = new Container(this);  
                DialogTemplate.InstantiateIn(_dialogContainer);  
     
                _dialogContainer.FormsGrid.MasterTableView.DataKeyNames = new string[] { "Key" };  
                _dialogContainer.FormsGrid.ItemCommand += new GridCommandEventHandler(FormsGrid_ItemCommand);  
                _dialogContainer.FormsGrid.NeedDataSource += new GridNeedDataSourceEventHandler(FormsGrid_NeedDataSource);  
                 
                Controls.Add(_dialogContainer);  
            }  
     
            #endregion  
     
            #region Private methods  
     
            private void BindGrid()  
            {  
                _dialogContainer.FormsGrid.DataSource = Value;  
                _dialogContainer.FormsGrid.DataBind();  
            }  
     
            #endregion  
     
            #region Event handlers  
     
            private void FormsGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
            {  
                _dialogContainer.FormsGrid.DataSource = Value;  
            }  
     
            private void FormsGrid_ItemCommand(object source, GridCommandEventArgs e)  
            {  
                string key = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["Key"].Text;  
                //if (listLinks.ContainsKey(key))  
                //    listLinks.Remove(key);  
                BindGrid();  
            }  
     
            #endregion  
        }  
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    29 Jan 2010
    Link to this post
    Hi bemara57,

    You have a slash in the name of the template instead of dot.

    MyNamespace.SomeProject.Forms.Resources.ControlTemplates.Backend/FormsListItemEditor.ascx

    Below is a code of a working sample

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using Telerik.Samples;
    using Telerik.Cms.Web.UI;
    using Telerik.Framework.Web;
     
    namespace ClassLibrary1
    {
        class CustomWebEditor : WebUITypeEditor<string>
        {
            // THIS DOES NOT DO ANYTHING JUST FOR THE TEST
            public override string Value
            {
                get
                {
                    return test;
                }
                set
                {
                    this.test = value;
                }
            }
     
            //SET TEMPLATE PATH
            [WebSysTemplate(CustomWebEditor.MySelectorTemplateName, "MySelector_Desc", "/MySelector", false, "2009-01-31")]
            public string TemplatePath
            {
                get
                {
                    return this.layoutTemplatePath;
                }
                set
                {
                    this.layoutTemplatePath = value;
                }
            }
     
            //SET TEMPLATE NAME
            protected virtual string LayoutTemplateName
            {
                get
                {
                    return CustomWebEditor.MySelectorTemplateName;
                }
            }
     
            //CREATE CONTAINER AND INSTANTIATE THE TEMPLATE
            // THIS WILL BE CALLED IN CREATECHILDCONTROLS
            protected virtual GenericContainer Container
            {
                get
                {
                    if (this.container == null)
                    {
                        this.container = new GenericContainer();
                        this.Template.InstantiateIn(this.container);
                    }
                    return this.container;
                }
            }
     
            // GET TEMPLATE
            public ITemplate Template
            {
                get
                {
                    if (this.template == null)
                        this.template = ControlUtils.GetTemplate(this.TemplatePath, this.LayoutTemplateName, this.GetType());
                    return this.template;
                }
                set
                {
                    this.template = value;
                }
            }
     
            //ADD THE CONTAINER
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                this.Controls.Add(this.Container);
            }
     
     
            //PRIVATE PROPERTIES
            private const string MySelectorTemplateName =
             "ClassLibrary1.Resources.WebEditorTempalte.ascx";
            private string layoutTemplatePath;
            private ITemplate template;
            private GenericContainer container;
            private string test;
     
     
            
        }
    }

    Note - the template should be build as embedded resource.

    All the best,
    Ivan Dimitrov
    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.
Register for webinar
4 posts, 0 answered