Handle content in usercontrols through CMS?

2019-07-07 04:55发布

Is there a way in Composite C1 to manage static text in ASP.NET usercontrols used on pages through the backend?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-07 05:33

In this answer I'm asuming you would like the "static text" to be maintained via a Visual Editor (html document), allowing your users to do heading, styling, bold etc. If you are just going for a simple large textbox this can be simplified.

Start by creating a new Global Data Type on the Data perspective - name it (in the code sample below, I named it Maw.Content) and give it these two fields:

  • FieldKey string(32) (Widget: the default TextBox)
  • FieldContent string(Unlimited) (Widget: Composite.Widgets.String.VisualXhtmlEditor)

Once you save your new data type you can add 'records' to it - and specify a field key and related content.

This should take care of managing the content - the UI you get should be pretty user friendly. You can right click the data type in the tree and use the command "Show in Content perspective" which will make your data type show up on Content | Website Items. This way your users do not have to use the Data perspective at all.

Consider limiting user access to the data folder to just "Edit", in case you do not need users to add/delete items. Right click the folder containing data items and slect 'Edit Permissions'.

Outputting the XHTML from a named field In your user controls you can grab the html related to s specific FieldKey like this:

using System;
using System.Linq;
using System.Xml.Linq;
using System.Web.UI;
using Composite.Data;
using Composite.Core.Xml;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string fieldKey = "SomeKeyHere";
        string xhtmlString;

        using( var connection = new DataConnection())
        {
            xhtmlString = connection.Get<Maw.Content>().Where(f => f.FieldKey == fieldKey).Select(f => f.FieldContent).FirstOrDefault();
        }

        if (xhtmlString != null)
        {
            XhtmlDocument htmlDoc = XhtmlDocument.Parse(xhtmlString);
            foreach (XNode bodyNode in htmlDoc.Body.Nodes())
            {
                this.Controls.Add( new LiteralControl(bodyNode.ToString()));
            }
        }
        else
        {
            this.Controls.Add(new LiteralControl("Unknown FieldKey: " + fieldKey));            
        }

    }
}
查看更多
登录 后发表回答