ASP.Net Binding Attributes of Own Internal Control Property

I created a UserControl with an internal property called "Actions", which is a list of "Action" objects. The code is as follows:

[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"">&nbsp;</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; }
}

If I then put this code in my aspx inside a DataRepeater, it works fine:

<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>

However, if I try to bind data to the attributes of the Action elements as follows:

<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>

I just get the actual text "<% # DataBinder.Eval (((RepeaterItem) Container.Parent) .DataItem," Url "%>" assigned to the Url property, not the expressed server expression as I expected.

I have been looking for this for several hours, but it seems I cannot find anyone else trying to do this. Any ideas why this doesn't work and how to get around it?

Thanks Bjorn

+3
2

, . , ASPX :

<uc1:MyLink runat="server" Url="/">
   <Actions>
      <uc1:Action Url="/Page.aspx?cpr=##cpr##&opgaveId=##id##" />
      <uc1:Action Url="/Test.aspx" />
   </Actions>
</uc1:MyLink>

CS, :

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();
}
0

DataRepeater.Datasource aspx ...

DataRepeater , , , page_load page_init

Action a;
foreach(object x in objects)
{
   a= new Action();
   a.Url = ... ;
   a.Text = ... ;
   MyLink.Actions.Add(a);
}

0

Source: https://habr.com/ru/post/1790935/


All Articles