Guest User

asp.net UserControl

a guest
Mar 23rd, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1.     [ParseChildren(true), PersistChildren(true)]
  2.     public class IfDiv : UserControl
  3.     {
  4.         [TemplateContainer(typeof(UserControl))]
  5.         public ITemplate Format { get; set; }
  6.  
  7.         [PersistenceMode(PersistenceMode.InnerProperty)]
  8.         public List<Item> Items { get; set; }
  9.  
  10.         //Constructor
  11.         public IfDiv()
  12.         {
  13.             Items = new List<Item>();
  14.             Format = new StringITemplate(String.Empty);
  15.         }
  16.  
  17.         //Methods
  18.         protected override void Render(HtmlTextWriter writer)
  19.         {
  20.             string output;
  21.  
  22.             if (!Items.Any() || Items.Any(x => String.IsNullOrWhiteSpace(ParseITemplate(x.Text)))) output = String.Empty;
  23.             else
  24.             {
  25.                 var formatParams = Items.Select(x => ParseITemplate(x.Text)).Cast<object>().ToArray();
  26.                 output = String.Format(ParseITemplate(Format), formatParams);
  27.             }
  28.  
  29.             writer.Write(output);
  30.         }
  31.  
  32.         private static string ParseITemplate(ITemplate iTemplate)
  33.         {
  34.             //Return an empty string when the ITemplate is null
  35.             if (iTemplate == null) return String.Empty;
  36.  
  37.             //Instantiate the template
  38.             var container = new UserControl();
  39.             iTemplate.InstantiateIn(container);
  40.             var stringWriter = new StringWriter();
  41.             container.RenderControl(new HtmlTextWriter(stringWriter));
  42.  
  43.             //Return
  44.             var output = stringWriter.ToString();
  45.             return output;
  46.         }
  47.     }
  48.  
  49.     public class StringITemplate : ITemplate
  50.     {
  51.         public string Template { get; set; }
  52.  
  53.         public StringITemplate(string template)
  54.         {
  55.             Template = template;
  56.         }
  57.  
  58.         public void InstantiateIn(Control container)
  59.         {
  60.             var page = HttpContext.Current.Handler as Page;
  61.             var tempContainer = page.ParseControl(Template);
  62.             container.Controls.Add(tempContainer);
  63.         }
  64.     }
  65.  
  66.     [ParseChildren(true)]
  67.     public class Item
  68.     {
  69.         //Properties
  70.         [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  71.         [TemplateContainer(typeof(UserControl))]
  72.         public ITemplate Text { get; set; }
  73.  
  74.         //Constructors
  75.         public Item() { }
  76.  
  77.         public Item(string text)
  78.         {
  79.             Text = new StringITemplate(text);
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment