Itemplate properties not available as attributes

1.1k views Asked by At

I've added an ITemplate to Telerik's RadGrid control called SearchMenuTemplate ala:

    public class AbsRadGrid : RadGrid
{

    private ITemplate _ItemTemplate;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(searchBar))]
    public ITemplate SearchMenuTemplate
    {
        get { return _ItemTemplate; }// get
        set { _ItemTemplate = value; }// set
    }
}

And the Template class looks something like (mandatory override methods like createchildcontrol have been omitted for brevity):

[ParseChildren(true)]
class searchBar : CompositeControl, INamingContainer
{
    public string rbStartsWithText { get; set; }
}

Now, in the source control window the RadGrid control sees the Template. But rbStartsWithText isn't an attribute on the node.

I want to see something like this (note: abs prefix is registered in the markup):

    <abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>

Instead rbStartsWithText is throwing a green squiggly and telling me it's not a valid attribute of SearchMenuTemplate.

1

There are 1 answers

0
Oleks On

Your SearchMenuTemplate property is of a type ITemplate which hasn't public properties, so IntelliSense just cannot offer any attribute for your <SearchMenuTemplate> tag.

To be able to add a custom property you should implement ITemplate interface (InstantiateIn method) and add desired property there:

public class YourCustomTemplate : ITemplate
{
    public string rbStartsWithText { get; set; }

    public void InstantiateIn(Control container)
    {
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.InnerText = rbStartsWithText;
        container.Controls.Add(div);
    }
}

then you could use it your custom grid:

public class AbsRadGrid : RadGrid
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public YourCustomTemplate  SearchMenuTemplate { get; set; }
}

and finally:

<abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>