Home All Groups Group Topic Archive Search About

Re: At which life cycle should EnsureChildControls() be called?

Author
21 Mar 2005 5:34 PM
Brock Allen
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



Show quote
> 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";
> }
> }
> }

Author
21 Mar 2005 5:42 PM
Jason Bentley
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
Author
21 Mar 2005 9:13 PM
Henry
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

Show quote
"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
>
>
Author
22 Mar 2005 12:53 PM
Teemu Keiski
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

Show quote
"Henry" <He***@discussions.microsoft.com> wrote in message
news:BC5C1AB0-6B53-4905-90B4-CAA73D8F6937@microsoft.com...
>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
>>
>>
Author
10 Apr 2005 6:40 AM
Guess
[This followup was posted to
microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
to the cited author.]

In article <enW1w4tLFHA.3***@TK2MSFTNGP09.phx.gbl>,
jot***@aspalliance.com 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.

AddThis Social Bookmark Button