1

I'm searching for 2 days now to accomplish the following: I want a custom control which will take inline text + code blocks.

Example of what I want to achieve:

<asp:MyCustom runat="server">
    Hello there, <%= User.Name%>
</asp:MyCustom>

Here is what I currently have:

[ParseChildren(true), PersistChildren(true)]
public class MyCustom
{
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public ITemplate Text { get; set; }

    //Constructor
    public MyCustom()
    {
        Text = new StringITemplate(text);
    }
}

On top of that, the control is actually a member of a list of a parent user control that inherits from "UserControl", overrides the Render method. Also, there is a class StringITemplate, which allows assigning plain strings to the ITemplate property as well.

Ultimately, I want to achieve the following:

<asp:MyCustomItemControl runat="server">
    <Items>
        <asp:Item>Hello there, <%= User.Name%></asp:Item>
        <asp:Item><%= SomeOtherString%></asp:Item>
    </Items>
</asp:MyCustom>

Right now, I would be happy if my first request actually worked. Seems like hell to get a grip on the wilderness of web/user/custom/server - controls in asp.net and their properties.

The complete code of what I have right now, can be looked at here: http://pastebin.com/3gic2Y0u

Thanks for reading :)

Edwin
  • 733
  • 8
  • 20

1 Answers1

2

I was just looking for something like this.

I found this answer here, which answered it for me:

How do I make my ASP.NET server control take an embedded code block as a property value?

A summary of the answer is: <%= type codeblocks are intended to be feed directly to the streamwriter. Instead, use <%# codeblock and include

This.DataBind()

in the Page_Init of the Item class.

Community
  • 1
  • 1
mrmillsy
  • 495
  • 3
  • 14