Is there a way in Composite C1 to manage static text in ASP.NET usercontrols used on pages through the backend?
1 Answers
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));
}
}
}

- 1,564
- 1
- 12
- 21
-
this works as long as there nothing in the head-element, and that *FieldContent* doesn't contain any functions itself. – Pauli Østerø Nov 28 '11 at 22:38
-
When retrieving the data via your example code, I get the problem that I can only add content that lies within subnodes. What if I have plaintext in the body? Or even better: How can I get the whole content of the body as a string (including the HTML tags)? I couldn't find a method for that. – magnattic Dec 20 '11 at 22:31
-
I have updated the code - it now use the Nodes() inside the body instead of just the Elements() - this should include comments and strings. Changes not tested, let me know I missed anything. – mawtex Dec 21 '11 at 15:51