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

Forums / Developing with Sitefinity / Custom meta-field control not set on edit

Custom meta-field control not set on edit

2 posts, 0 answered
  1. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    07 Jul 2011
    Link to this post
    I have two custom meta-fields in a generic content module, and am trying to create an editor to edit one of the two fields based on some configuration.

    To do this I have created a control which inherits from ITextControl and added this to the ContentMetaFields ItemTemplate in my edit view (.ascx):

    <sf:ContentMetaFields id="MetaFields" runat="server">
        <ItemTemplate>

            <ctrl:CustomMetaFieldControl runat="server" />

        </ItemTemplate>
    </sf:ContentMetaFields>

    I am not specifying an ID on the control because I want to set the ID inside the control to allow me to control what field is populated. I am able to set a value and see that it is being saved against the generic content, however what I cannot get it to do is re-populate the field when editing. For some reason it doesn't seem to be calling set on the Text property.

    Here is the code from my control:

    public class CustomMetaFieldControl : CompositeControl, ITextControl
        {
            private TextBox textbox;
     
            protected override void OnInit(System.EventArgs e)
            {
                this.ID = true ? "MetaField1" : "MetaField2";
                base.OnInit(e);
            }
     
            protected override void CreateChildControls()
            {
                textbox = new TextBox();
                this.Controls.Add(textbox);
                base.CreateChildControls();
            }
     
            public string Text
            {
                get { return textbox.Text; }
                set { textbox.Text = value; }
            }
        }


    If I set the meta field name on the control mark up then it does work, but I don't want to put the logic for selecting which meta field to display into the view.

    So to re-cap, saving a new value works, but when I edit the generic content the old value is not pre-populated in the textbox.

    Is there a way I can get this working which will allow me to keep all the logic in the control, rather than changing the view to say if (x) { show field 1 } else { show field 2 }?
  2. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    07 Jul 2011
    Link to this post
    I've got this working by creating a constructor and setting the ID in there. I figured it was a lifecycle issue.

    It doesn't seem like a particularly great thing to do but I'm happy to do this if it's the only way. Is there any reason you think I should not do this?
2 posts, 0 answered