Template control error

T

timor.super

Hi group,

i'm trying to create a template control, very easy with nothing else a
button in it :



<Test:TestTemplate ID="test" runat="server" />



public class TestTemplate : WebControl, INamingContainer
{
private ITemplate _template;
private TestPanel _panel;

public TestPanel Panel
{
get { EnsureChildControls(); return _panel; }
}

public virtual ITemplate Template
{
get { return _template; }
set { _template = value; }
}

public override ControlCollection Controls
{
get { EnsureChildControls(); return base.Controls; }
}

protected override void CreateChildControls()
{
Controls.Clear();
_panel = new TestPanel();
ITemplate template = Template;
if (template == null)
{
template = new DefaultTemplate();
}
Button b = new Button();
b.Text = "OK";
template.InstantiateIn(_panel);
Controls.Add(_panel);
Controls.Add(b);
}

public override void DataBind()
{
CreateChildControls();
ChildControlsCreated = true;
base.DataBind();
}

private sealed class DefaultTemplate : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
}
}
}

public class TestPanel : Panel, INamingContainer
{
}



When I click on the button, after postback, I have a
NullReferenceException

the call stack :

[NullReferenceException:]
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +93

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+72
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+3839


What should I do ?



Thanks for your help.



Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top