Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [ParseChildren(true), PersistChildren(true)]
- public class IfDiv : UserControl
- {
- [TemplateContainer(typeof(UserControl))]
- public ITemplate Format { get; set; }
- [PersistenceMode(PersistenceMode.InnerProperty)]
- public List<Item> Items { get; set; }
- //Constructor
- public IfDiv()
- {
- Items = new List<Item>();
- Format = new StringITemplate(String.Empty);
- }
- //Methods
- protected override void Render(HtmlTextWriter writer)
- {
- string output;
- if (!Items.Any() || Items.Any(x => String.IsNullOrWhiteSpace(ParseITemplate(x.Text)))) output = String.Empty;
- else
- {
- var formatParams = Items.Select(x => ParseITemplate(x.Text)).Cast<object>().ToArray();
- output = String.Format(ParseITemplate(Format), formatParams);
- }
- writer.Write(output);
- }
- private static string ParseITemplate(ITemplate iTemplate)
- {
- //Return an empty string when the ITemplate is null
- if (iTemplate == null) return String.Empty;
- //Instantiate the template
- var container = new UserControl();
- iTemplate.InstantiateIn(container);
- var stringWriter = new StringWriter();
- container.RenderControl(new HtmlTextWriter(stringWriter));
- //Return
- var output = stringWriter.ToString();
- return output;
- }
- }
- public class StringITemplate : ITemplate
- {
- public string Template { get; set; }
- public StringITemplate(string template)
- {
- Template = template;
- }
- public void InstantiateIn(Control container)
- {
- var page = HttpContext.Current.Handler as Page;
- var tempContainer = page.ParseControl(Template);
- container.Controls.Add(tempContainer);
- }
- }
- [ParseChildren(true)]
- public class Item
- {
- //Properties
- [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
- [TemplateContainer(typeof(UserControl))]
- public ITemplate Text { get; set; }
- //Constructors
- public Item() { }
- public Item(string text)
- {
- Text = new StringITemplate(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment