Advertisement
Guest User

ASP.Net binding attributes of a custom control's inner property

a guest
Mar 23rd, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. [ParseChildren(true)]
  2. public class MyLink : UserControl
  3. {
  4. readonly List<Action> _actions = new List<Action>();
  5.  
  6. [PersistenceMode(PersistenceMode.InnerProperty)]
  7. public List<Action> Actions
  8. {
  9. get { return _actions; }
  10. }
  11.  
  12. public string Text { get;set; }
  13. public string Url { get;set; }
  14. public string MenuName { get; set; }
  15.  
  16. protected override void Render(HtmlTextWriter writer)
  17. {
  18. //Build link
  19. StringBuilder sb = new StringBuilder();
  20. sb.Append(@"
  21. <table class=""myLink"">
  22. <tr>
  23. <td class=""myLinkLeft""><a href=" + Url + @">" + Text + @"</a></td>
  24. <td class=""myLinkRight " + MenuName + @"_trigger"">&nbsp;</td>
  25. </tr>
  26. </table>
  27. ");
  28.  
  29. //Build actions
  30. sb.Append("<ul id="" + MenuName + "_actions" class="contextMenu">");
  31.  
  32. foreach (Action action in _actions)
  33. {
  34. sb.Append("<li class="" + action.CssClass + ""><a href="#" + action.Url + "">" + action.Text + "</a></li>");
  35. }
  36.  
  37. sb.Append("</ul>");
  38.  
  39. writer.Write(sb.ToString());
  40. }
  41. }
  42.  
  43. public class Action : UserControl
  44. {
  45. public string Url { get; set; }
  46. public string Text { get; set; }
  47. public string ImageUrl { get; set; }
  48. public string CssClass { get; set; }
  49. }
  50.  
  51. <uc1:MyLink runat="server" Url="/" Text='<%#DataBinder.Eval(Container.DataItem,"Text") %>' MenuName="contextMenu" id="contextMenu">
  52. <Actions>
  53. <uc1:Action runat="server" Url="http://mysite.com" Text="MyUrl" />
  54. <uc1:Action runat="server" Url="http://google.com" Text="Google" />
  55. </Actions>
  56. </uc1:MyLink>
  57.  
  58. <uc1:MyLink runat="server" Url="/" Text='<%#DataBinder.Eval(Container.DataItem,"Text") %>' MenuName="contextMenu" id="contextMenu">
  59. <Actions>
  60. <uc1:Action runat="server" Url='<%#DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem,"Url") %>' Text="MyUrl" />
  61. <uc1:Action runat="server" Url="http://google.com" Text="Google" />
  62. </Actions>
  63. </uc1:MyLink>
  64.  
  65. Action a;
  66. foreach(object x in objects)
  67. {
  68. a= new Action();
  69. a.Url = ... ;
  70. a.Text = ... ;
  71. MyLink.Actions.Add(a);
  72. }
  73.  
  74. <uc1:MyLink runat="server" Url="/">
  75. <Actions>
  76. <uc1:Action Url="/Page.aspx?cpr=##cpr##&opgaveId=##id##" />
  77. <uc1:Action Url="/Test.aspx" />
  78. </Actions>
  79. </uc1:MyLink>
  80.  
  81. protected override void OnInit(EventArgs e)
  82. {
  83. DataBinding += BindData;
  84. }
  85.  
  86. public void BindData(object sender, EventArgs e)
  87. {
  88. MyLink pl = (MyLink) sender;
  89. IDataItemContainer container = (IDataItemContainer) pl.NamingContainer;
  90. foreach (Action action in _actions)
  91. {
  92. action.Url = ReplaceTokens(action.Url, container);
  93. action.Text = ReplaceTokens(action.Text, container);
  94. }
  95. }
  96.  
  97. private static string ReplaceTokens(string text, IDataItemContainer container)
  98. {
  99. Regex re = new Regex("##.*?##", RegexOptions.Compiled | RegexOptions.Singleline);
  100. StringBuilder sb = new StringBuilder(text);
  101.  
  102. foreach (Match m in re.Matches(text))
  103. {
  104. string tokenValue = DataBinder.GetPropertyValue(container.DataItem, m.Value.Substring(2, m.Value.Length - 4), "{0}");
  105. sb.Replace(m.Value, tokenValue);
  106. }
  107. return sb.ToString();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement