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

Forums / Developing with Sitefinity / Storing comma delimited string in metakey

Storing comma delimited string in metakey

6 posts, 0 answered
  1. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    10 Dec 2010
    Link to this post
    Hello,

    I have a MetaKey in one of my modules that needs to store multiple values, i.e.:

    What formats do you record in?: CD / MP3 / Cassette

    I thought the easiest way to do this would be to use a CheckBoxList wrapped in a user control which inherits from ITextControl and exposes the Text property.

    The problem is when I click "Save changes" SiteFinity only seems to pick up the first checkbox that was selected, not all of them. I therefore presume I need to:

    - Override SaveContent and loop through the checkboxes checking against the Checked property.
    - Override the SetContent method and loop through the comma delimited string, setting the Checked property of the checkbox list appropriately.

    If this is correct, could someone please explain how to set the value of a metafield from within the savecontent method? The method does not take an input of IContent or similar so there is no obvious content to get a handle on.

    Thanks all

    higgsy
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Dec 2010
    Link to this post
    Hi higgsy,

    Inside SaveContent you can use this.DataItemId which gives you the ID of the IContent item. Another option is using static event Executed of ContentManager inside Global.asax - Application_Start where you have access to the item ID from the command arguments.

    Inside the view (ContentItemEdit is the base view for all edit views )there is a virtual method - SetContentForm which you can use to select the proper value from your CheckBoxList when you open an item for editing. Here you need to parse the delimited value.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    10 Dec 2010
    Link to this post
    Hi Ivan,

    Thanks for your response. The problem is that on the NewView, the DataitemId is Guid.Empty.

    The overridden method for savecontent is as so:

    protected override void SaveContent()
            {
     
                Context.Response.Write("The value is " + this.DataItemId);
                Context.Response.End();
                     
                base.SaveContent();
     
            }

    So I don't see how I can update the metakey before the form is submitted?

    Thanks
    higgsy
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Dec 2010
    Link to this post
    Hi higgsy,

    Try using global.asax for newly created items or Create and Save the content manually inside SaveContent() ( do not call the base class in this case) where you use this.Manager.CreateContent("text/html");

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Leonard
    Leonard avatar
    19 posts
    Registered:
    01 Dec 2005
    11 Dec 2010
    Link to this post
    Higgsy,

    As you mentioned, you already have the CheckBoxList wrapped in a user control which inherits from ITextControl and exposes the Text property. This property would hold your delimited string of values corresponding to the "checked" status of each check-box, and it's the same value that will be persisted to the related metakey value when you save the changes (standard functionality).

    In order to have the standard functionality handle the metakey maintenance automatically, it's enough to ensure the public Text property of your user control has the correct value at all times.

    To enable this behaviour, wrap the CheckBoxList  in a RadAjaxPanel, set AutoPostBack to "true" for your CheckBoxList, and in its OnSelectedIndexChanged event update the public Text property of your user control to reflect the current selection.

    [no need to override any base methods, etc.]

    I hope this helps --


  6. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    11 Dec 2010
    Link to this post
    Hi all,

    Thanks for your responses. For anyone that would like the full example of how I achieved this, please see code posted below:

    Create a user control that exposes the Text property. The user control contains a CheckBoxList control.

    CheckBoxListControl.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CheckBoxListControl.ascx.cs" Inherits="lib_controls_dialogs_editors_backend_CheckBoxListControl" %>
    <asp:CheckBoxList ID="radRecordingFormats" runat="server" CssClass="checkBox checkBoxPosition" AppendDataBoundItems="false">
        <asp:ListItem Text="0" Value="0" />
    </asp:CheckBoxList>

    Code-behind
    CheckBoxListControl.ascx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    public partial class lib_controls_dialogs_editors_backend_CheckBoxListControl : System.Web.UI.UserControl, ITextControl
    {
     
        public string Text { get; set; }
     
        protected void Page_Load(object sender, EventArgs e)
        {
     
            if (!IsPostBack) {
     
                //create a data source for the xmnl recording formats
                XmlDataSource oFormatsXmlDataSource = new XmlDataSource();
                oFormatsXmlDataSource.DataFile = "~/lib/xml/RecordingFormats.xml";
                oFormatsXmlDataSource.XPath = "/root/format";
     
                //bind the dropdown list
                this.radRecordingFormats.ClearSelection();
                this.radRecordingFormats.DataTextField = "name";
                this.radRecordingFormats.DataValueField = "name";
                this.radRecordingFormats.DataSource = oFormatsXmlDataSource;
                this.radRecordingFormats.DataBind();
     
                if (!string.IsNullOrEmpty(this.Text)) {
     
                    //convert into comma-delimited string
                    string[] arrItems = Text.ToString().Split(',');
     
                    foreach (string strItem in arrItems) {
     
                        foreach (ListItem oListItem in this.radRecordingFormats.Items) {
     
                            if (string.Compare(oListItem.Value, strItem) == 0)
                                oListItem.Selected = true;
     
                        }
     
                    }
     
                }
     
     
            }
        }
    }

    Then in your module user control i.e. add / edit views, register the user control:

    <%@ Register TagPrefix="mycontrols" TagName="CheckBoxListControl" Src="~/lib/controls/dialogs/editors/backend/CheckBoxListControl.ascx"  %>

    And then wherever you want the CheckBoxList control to appear:

    <mycontrols:CheckBoxListControl runat="server" ID="RecordingFormats" />

    Then finally, in your module's EditView.cs or NewView.cs you need to update your metafield (in my example it was a field called RecordingFormats which I wanted to store a comma delimited string). You can do this easily by overriding the SaveContent method:

    //store selected items in a list
                            List<string> oSelectedItems = new List<string>();
     
                            //loop through each item in checkboxlist checking whether it is selected
                            foreach (ListItem oItem in oRecordingFormatsCheckBoxList.Items) {
     
                                if (oItem.Selected) {
                                    oSelectedItems.Add(oItem.Value);
                                }
     
                            }
     
                            //set Text property of our RecordingFormats user control
                            ((ITextControl)this.oRecordingFormatsUserControl).Text = string.Join(",", oSelectedItems.ToArray());
     
                            //save content
                            base.SaveContent();

    Although, the post above by Leonard suggests using a RadAjaxPanel to update the "Text" property which may work even better.

    Hope this helps someone else!

    higgsy
Register for webinar
6 posts, 0 answered