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