Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [ParseChildren(true)]
- public class MyLink : UserControl
- {
- readonly List<Action> _actions = new List<Action>();
- [PersistenceMode(PersistenceMode.InnerProperty)]
- public List<Action> Actions
- {
- get { return _actions; }
- }
- public string Text { get;set; }
- public string Url { get;set; }
- public string MenuName { get; set; }
- protected override void Render(HtmlTextWriter writer)
- {
- //Build link
- StringBuilder sb = new StringBuilder();
- sb.Append(@"
- <table class=""myLink"">
- <tr>
- <td class=""myLinkLeft""><a href=" + Url + @">" + Text + @"</a></td>
- <td class=""myLinkRight " + MenuName + @"_trigger""> </td>
- </tr>
- </table>
- ");
- //Build actions
- sb.Append("<ul id="" + MenuName + "_actions" class="contextMenu">");
- foreach (Action action in _actions)
- {
- sb.Append("<li class="" + action.CssClass + ""><a href="#" + action.Url + "">" + action.Text + "</a></li>");
- }
- sb.Append("</ul>");
- writer.Write(sb.ToString());
- }
- }
- public class Action : UserControl
- {
- public string Url { get; set; }
- public string Text { get; set; }
- public string ImageUrl { get; set; }
- public string CssClass { get; set; }
- }
- <uc1:MyLink runat="server" Url="/" Text='<%#DataBinder.Eval(Container.DataItem,"Text") %>' MenuName="contextMenu" id="contextMenu">
- <Actions>
- <uc1:Action runat="server" Url="http://mysite.com" Text="MyUrl" />
- <uc1:Action runat="server" Url="http://google.com" Text="Google" />
- </Actions>
- </uc1:MyLink>
- <uc1:MyLink runat="server" Url="/" Text='<%#DataBinder.Eval(Container.DataItem,"Text") %>' MenuName="contextMenu" id="contextMenu">
- <Actions>
- <uc1:Action runat="server" Url='<%#DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem,"Url") %>' Text="MyUrl" />
- <uc1:Action runat="server" Url="http://google.com" Text="Google" />
- </Actions>
- </uc1:MyLink>
- Action a;
- foreach(object x in objects)
- {
- a= new Action();
- a.Url = ... ;
- a.Text = ... ;
- MyLink.Actions.Add(a);
- }
- <uc1:MyLink runat="server" Url="/">
- <Actions>
- <uc1:Action Url="/Page.aspx?cpr=##cpr##&opgaveId=##id##" />
- <uc1:Action Url="/Test.aspx" />
- </Actions>
- </uc1:MyLink>
- protected override void OnInit(EventArgs e)
- {
- DataBinding += BindData;
- }
- public void BindData(object sender, EventArgs e)
- {
- MyLink pl = (MyLink) sender;
- IDataItemContainer container = (IDataItemContainer) pl.NamingContainer;
- foreach (Action action in _actions)
- {
- action.Url = ReplaceTokens(action.Url, container);
- action.Text = ReplaceTokens(action.Text, container);
- }
- }
- private static string ReplaceTokens(string text, IDataItemContainer container)
- {
- Regex re = new Regex("##.*?##", RegexOptions.Compiled | RegexOptions.Singleline);
- StringBuilder sb = new StringBuilder(text);
- foreach (Match m in re.Matches(text))
- {
- string tokenValue = DataBinder.GetPropertyValue(container.DataItem, m.Value.Substring(2, m.Value.Length - 4), "{0}");
- sb.Replace(m.Value, tokenValue);
- }
- return sb.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement