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

Forums / General Discussions / How to retrieve meta data into SimpleControl (for custom module)?

How to retrieve meta data into SimpleControl (for custom module)?

2 posts, 0 answered
  1. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    25 Jan 2010
    Link to this post

    I created a SimpleControl that I want for it to grab a meta data from the database, but it doesn't grab it. When I replace the below control with <asp:TextBox ID="SimpleData" runat="server"></asp:TextBox>, it populates it perfectly.. So I don't know what I am doing wrong with my SimpleControl. By the way, I am able to save the meta data just fine using this control- it's the retrieval that I'm having a problem with. Here's what my markup code looks like:

     

    <sf:ContentMetaFields id="MetaFields" runat="server">  
     <ItemTemplate> 
      ...  
      <fb:MySimpleControl ID="SimpleData" runat="server"></fb:MySimpleControl> 
      ...  
     </ItemTemplate> 
    </sf:ContentMetaFields> 

    In MySimpleControl, it looks like this:

        public class MySimpleControl : SimpleControl, ITextControl  
        {  
            #region Properties  
     
            /// <summary> 
            /// Name of the provider to be used with this control  
            /// </summary> 
            public string ProviderName  
            {  
                get  
                {  
                    return this.providerName;  
                }  
                set  
                {  
                    this.providerName = value;  
                }  
            }  
     
            /// <summary> 
            /// Gets or sets path of the external template that defines child controls of this control.  
            /// </summary> 
            [WebSysTemplate(MySimpleControl.ContactLayoutTemplateName, "MySimpleControl_Template_Desc", "/MySimpleTest/", true, "2009-02-20")]  
            public override string LayoutTemplatePath  
            {  
                get  
                {  
                    return base.LayoutTemplatePath;  
                }  
                set  
                {  
                    base.LayoutTemplatePath = value;  
                }  
            }  
     
            /// <summary> 
            /// Gets the name of the embedded layout template. This property must be overridden to provide the path (key) to an embedded resource file.   
            /// </summary> 
            public override string LayoutTemplateName  
            {  
                get  
                {  
                    return MySimpleControl.ContactLayoutTemplateName;  
                }  
            }  
     
            /// <summary> 
            /// Gets the additional template key.  
            /// </summary> 
            /// <value>The additional template key.</value> 
            protected virtual string AdditionalTemplateKey  
            {  
                get  
                {  
                    return this.ProviderName;  
                }  
            }  
     
            /// <summary> 
            /// Gets the form code.  
            /// </summary> 
            /// <value>The form code.</value> 
            protected virtual ITextControl txtForm  
            {  
                get  
                {  
                    return base.Container.GetControl<ITextControl>("txtForm", true);  
                }  
            }  
     
            #endregion  
     
            #region Control References  
     
            public string Text  
            {  
                get { return txtForm.Text; }  
                set { txtForm.Text = value; }  
            }  
     
            #endregion  
     
            #region Methods  
     
            /// <summary> 
            /// We don't want a wrapper tag around this control, therefore  
            /// we override these two methods and do nothing in them.  
            /// </summary> 
            /// <param name="writer"></param> 
            public override void RenderBeginTag(HtmlTextWriter writer)  
            {  
                //Do not render  
            }  
     
            public override void RenderEndTag(HtmlTextWriter writer)  
            {  
                //Do not render  
            }  
     
            /// <summary> 
            /// Called by ASP.NET when the control is being initialized.  
            /// Since we need ControlState for this control we'll register that need here  
            /// </summary> 
            /// <param name="e"></param> 
            protected override void OnInit(EventArgs e)  
            {  
                base.OnInit(e);  
                if (base.Page != null)  
                {  
                    base.Page.RegisterRequiresControlState(this);  
                }  
            }  
     
            /// <summary> 
            /// Saves values that we need to persist on postbacks to control state  
            /// </summary> 
            /// <returns></returns>  
            protected override object SaveControlState()  
            {  
                return new object[]   
       {  
        this.providerName  
       };  
            }  
     
            /// <summary> 
            /// Loads persisted values from control state so we can work with them  
            /// </summary> 
            /// <param name="savedState"></param> 
            protected override void LoadControlState(object savedState)  
            {  
                if (savedState != null)  
                {  
                    object[] state = (object[])savedState;  
                    this.providerName = (string)state[0];  
                }  
            }  
     
            protected override void InitializeControls(Control controlContainer)  
            {  
                base.InitializeControls(controlContainer);  
            }  
     
            #endregion  
     
            #region Private fields  
     
            private string providerName;  
     
            #endregion  
     
            #region Constants  
     
            public const string ContactLayoutTemplateName = "MySimpleNamespace.Resources.ControlTemplates.Backend.MySimpleControl.ascx";  
     
            #endregion  
        }  
     

    Notice how I inherit the ITextControl interface. What am I missing??

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    26 Jan 2010
    Link to this post
    Hi bemara57,

    You cannot inherit from Simple control because the ChildControls are cleared and there is no value set to your control when you edit a content item.

    Possible solutions

    1. Inherit from CompositeControl, ITextControl and create your controls dynamically.

    SAMPLE

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Web.UI;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    /// <summary>
    /// Summary description for MetakeysItextControl
    /// </summary>
    ///
     
    namespace Telerik.Samples
    {
        public class MetakeysItextControl : CompositeControl, ITextControl
        {
            public MetakeysItextControl()
            {
            }
     
     
            protected override void CreateChildControls()
            {
                this.Controls.Add(TB);
            }
     
     
            private GenericContainer container;
            private TextBox tb;
            private TextBox TB
            {
                get
                {
                    if (this.tb == null)
                        this.tb = new TextBox();
                    return this.tb;
                }
            }
     
            //private string text;
     
            #region ITextControl Members
     
            public string Text
            {
                get
                {
                    return this.TB.Text;
                }
                set
                {
                    this.TB.Text = value;
                }
            }
     
     
            #endregion
        }
    }

    2. Create a class that inherits from CompositeControl, ITextControl and implement your own container and instinate your custom template.

    3. When you edit the item get the item id from the QueryString  Param=47aaf50c-b840-4219-a11f-64dfca5cf06e and use Manager.GetContent(PASS THE ID HERE). Then use GetMetaData to retrieve the value.

    Greetings,
    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.
2 posts, 0 answered