|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Re: At which life cycle should EnsureChildControls() be called?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"; > } > } > } 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 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 > > 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. -- Show quoteTeemu Keiski ASP.NET MVP, Finland "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 >> >> [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 Checkboxes only receive values when satisfied.> (state loading for checkbox) works based on postback data. That is, set > Checked property to false manually at some stage on your control/page. > > |
|||||||||||||||||||||||