At which life cycle should EnsureChildControls() be called?

H

Henry

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";
}
}
}
 
J

Jason Bentley

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
 
B

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.
 
H

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
 
T

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.
 
G

Guess

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

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top