At which life cycle should EnsureChildControls() be called?

Discussion in 'ASP .Net Web Controls' started by Henry, Mar 19, 2005.

  1. Henry

    Henry Guest

    I have a web custom control that contains a checkbox and a button.
    1) The check box needs to be initialized to unchecked state during page
    postbacks.
    2) The button has a click event handler.
    If I add "EnsureChildControls()" at OnLoad, checkbox is set to unchecked
    every time there is a post back, but the button click event handler is never
    called.
    If I remove "EnsureChildControls()" at OnLoad, checkbox retains its state
    during post backs, yet the button click event handler is called.

    How can I do both?

    Many Thanks. --Henry.

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.ComponentModel;

    namespace Henry.ControlSamples
    {
    [DefaultProperty("Text"),
    ToolboxData("<{0}:WebCustomControl1 runat=server></0} :WebCustomControl1>")]
    public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
    {
    protected override void OnLoad(EventArgs e)
    {
    //EnsureChildControls();
    //if EnsureChildControls() is commented out, button click event cannot be
    caught.
    //if EnsureChildControls() is NOT commented out,
    //during page postback, checkBox cannot be initialized to unchecked state
    if it is previousely checked.
    }

    protected override void CreateChildControls()
    {
    Controls.Clear();

    HtmlInputCheckBox checkBox = new HtmlInputCheckBox();
    Controls.Add(checkBox);

    Button b = new Button();
    Controls.Add(b);

    b.ID = "mytestbutton";
    b.Text = "test";
    b.Click+=new EventHandler(b_Click);
    }

    private void b_Click(object sender, EventArgs e)
    {
    ((Button) FindControl("mytestbutton")).Text = "test2";
    }
    }
    }
     
    Henry, Mar 19, 2005
    #1
    1. Advertising

  2. Henry, I am not 100% positive about this but PreRender() seems like the
    logical place. I am sure that Load is not the correct place so it
    should fall after load and after postback which would be PreRender().

    Jason Bentley
    http://geekswithblogs.net/jbentley
     
    Jason Bentley, Mar 21, 2005
    #2
    1. Advertising

  3. Henry

    Brock Allen Guest

    Try EnsureChildControls() in Page_Init -- this will recreate the server controls
    prior to the stage where ASP.NET reads the post data and view state and assigns
    it all into the server controls.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I have a web custom control that contains a checkbox and a button.
    > 1) The check box needs to be initialized to unchecked state during
    > page
    > postbacks.
    > 2) The button has a click event handler.
    > If I add "EnsureChildControls()" at OnLoad, checkbox is set to
    > unchecked
    > every time there is a post back, but the button click event handler is
    > never
    > called.
    > If I remove "EnsureChildControls()" at OnLoad, checkbox retains its
    > state
    > during post backs, yet the button click event handler is called.
    > How can I do both?
    >
    > Many Thanks. --Henry.
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    > using System.ComponentModel;
    > namespace Henry.ControlSamples
    > {
    > [DefaultProperty("Text"),
    > ToolboxData("<{0}:WebCustomControl1 runat=server></0}
    > :WebCustomControl1>")]
    > public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
    > {
    > protected override void OnLoad(EventArgs e)
    > {
    > //EnsureChildControls();
    > //if EnsureChildControls() is commented out, button click event
    > cannot be
    > caught.
    > //if EnsureChildControls() is NOT commented out,
    > //during page postback, checkBox cannot be initialized to unchecked
    > state
    > if it is previousely checked.
    > }
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    > HtmlInputCheckBox checkBox = new HtmlInputCheckBox();
    > Controls.Add(checkBox);
    > Button b = new Button();
    > Controls.Add(b);
    > b.ID = "mytestbutton";
    > b.Text = "test";
    > b.Click+=new EventHandler(b_Click);
    > }
    > private void b_Click(object sender, EventArgs e)
    > {
    > ((Button) FindControl("mytestbutton")).Text = "test2";
    > }
    > }
    > }
     
    Brock Allen, Mar 21, 2005
    #3
  4. You are right Brock. I misread the question. I thought he was asking
    about CreateChildControls(). EnsureChildControls() should definitely go
    in Page_Init.

    Jason Bentley
    http://geekswithblogs.net/jbentley
     
    Jason Bentley, Mar 21, 2005
    #4
  5. Henry

    Henry Guest

    I moved EnsureChildControls() to the page init method. I still have the
    problem of not being able to reset the check box to unchecked state during
    page post backs.
    The check box retains its previous state before the post back.

    Thanks.

    Henry

    "Jason Bentley" wrote:

    > You are right Brock. I misread the question. I thought he was asking
    > about CreateChildControls(). EnsureChildControls() should definitely go
    > in Page_Init.
    >
    > Jason Bentley
    > http://geekswithblogs.net/jbentley
    >
    >
     
    Henry, Mar 21, 2005
    #5
  6. Henry

    Teemu Keiski Guest

    For the checkbox you can nothing except unchecking it manually because it
    (state loading for checkbox) works based on postback data. That is, set
    Checked property to false manually at some stage on your control/page.

    --
    Teemu Keiski
    ASP.NET MVP, Finland

    "Henry" <> wrote in message
    news:...
    >I moved EnsureChildControls() to the page init method. I still have the
    > problem of not being able to reset the check box to unchecked state during
    > page post backs.
    > The check box retains its previous state before the post back.
    >
    > Thanks.
    >
    > Henry
    >
    > "Jason Bentley" wrote:
    >
    >> You are right Brock. I misread the question. I thought he was asking
    >> about CreateChildControls(). EnsureChildControls() should definitely go
    >> in Page_Init.
    >>
    >> Jason Bentley
    >> http://geekswithblogs.net/jbentley
    >>
    >>
     
    Teemu Keiski, Mar 22, 2005
    #6
  7. Henry

    Guess Guest

    [This followup was posted to
    microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
    to the cited author.]

    In article <>,
    says...
    > For the checkbox you can nothing except unchecking it manually because it
    > (state loading for checkbox) works based on postback data. That is, set
    > Checked property to false manually at some stage on your control/page.
    >
    >

    Checkboxes only receive values when satisfied.
     
    Guess, Apr 10, 2005
    #7
    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. john doe Jr

    ASP Life Cycle Problem

    john doe Jr, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    819
    Teemu Keiski
    Dec 5, 2003
  2. S. Justin Gengo

    Re: ASP Life Cycle Problem

    S. Justin Gengo, Dec 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    S. Justin Gengo
    Dec 8, 2003
  3. prem

    ASP.Net Life Cycle

    prem, Nov 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    490
    Kevin Spencer
    Nov 2, 2004
  4. Stephen Miller

    Why is SaveViewState called twice in the Control Life Cycle?

    Stephen Miller, Dec 31, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    500
    Teemu Keiski
    Jan 1, 2004
  5. TS
    Replies:
    3
    Views:
    1,835
Loading...

Share This Page