Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to write a databound control, SimpleControl, with an ITemplate named ItemViewTemplate. This control exposes a property of type SimpleItem. When this property is set, the controls within the SimpleControl are populated.

This all works fine when I put the control in the aspx file with no ItemViewTemplate markup. Once I put in the ItemViewTemplate markup, I get an error:
Cannot convert type 'System.Web.UI.Control' to 'Controls.Web.SimpleControlTemplate'. 

Below is my code.

Thanks in advance for your help.

SimpleControl.cs
C#
public class SimpleControl : CompositeControl
   {
       ITemplate _ItemViewTemplate = null;
       SimpleControlContainer _ItemContainer;

       [TemplateContainer(typeof(SimpleControlTemplate))]
       [PersistenceMode(PersistenceMode.InnerProperty)]
       public ITemplate ItemViewTemplate
       {
           get { return _ItemViewTemplate; }
           set { _ItemViewTemplate = value; }
       }

       public SimpleItem Item
       {
           get
           {
               if (ViewState["Item"] == null) ViewState["Item"] = new SimpleItem();
               return ViewState["Item"] as SimpleItem;
           }
           set
           {
               ViewState["Item"] = value;
           }
       }

       protected override void CreateChildControls()
       {
           _ItemContainer = new SimpleControlContainer();

           if (this._ItemViewTemplate == null)
           {
               this._ItemViewTemplate = new SimpleControlTemplate(_ItemContainer);
           }

           this._ItemViewTemplate.InstantiateIn(this);
           base.CreateChildControls();
       }
   }

SimpleControlContainer.cs
C#
public sealed class SimpleControlContainer : CompositeControl
{
    TextBox t;
    protected override void CreateChildControls()
    {
        t = new TextBox();
        t.ID = "txt";
        t.DataBinding += new EventHandler(t_DataBinding);

        this.Controls.Add(t);
        base.CreateChildControls();
    }

    void t_DataBinding(object sender, EventArgs e)
    {
        SimpleControl sc = (SimpleControl)this.NamingContainer;
        this.t.Text = sc.Item.Name;
    }
}

SimpleControlTemplate.cs
C#
public sealed class SimpleControlTemplate : ITemplate, INamingContainer
{
    private SimpleControlContainer sContainer;

    public SimpleControlTemplate(SimpleControlContainer container)
    {
        this.sContainer = container;
    }

    public void InstantiateIn(Control container)
    {
        container.Controls.Add(sContainer);
    }
}

SimpleItem.cs
C#
[Serializable]
public class SimpleItem
{
    public string Name { get; set; }
}

View.aspx.cs
C#
public partial class Items_View : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        this.SimpleControl1.Item.Name = "test";
        this.SimpleControl1.DataBind();
        base.OnLoad(e);
    }
}

View.aspx (this code binds correctly)
C#
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
         <cc1:SimpleControl ID="SimpleControl1"  runat="server">
         </cc1:SimpleControl>
    </form>
    </body>
</html>

View.aspx (this code gives me error)
C#
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
         <cc1:SimpleControl ID="SimpleControl1"  runat="server">
             <ItemViewTemplate>
                  <asp:TextBox ID="t" runat="server" Text='<%#Eval("Name") %>' />
             </ItemViewTemplate>
         </cc1:SimpleControl>
    </form>
    </body>
</html>
Posted
Updated 6-Jan-12 19:54pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900