Template control error

Discussion in 'ASP .Net' started by timor.super@gmail.com, Mar 20, 2008.

  1. Guest

    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
     
    , Mar 20, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    493
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    556
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,137
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    801
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    549
Loading...

Share This Page