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
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
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
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
[Serializable]
public class SimpleItem
{
public string Name { get; set; }
}
View.aspx.cs
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)
<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)
<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>