mercredi 20 avril 2016

CheckBox CheckedChanged event added from code behind

I have a couple of checkboxes that I add from my code behind, but I can't get the CheckedChanged event to fire.

This code is being called on page load:

CheckBox cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += cb_CheckedChanged;

//cb.CheckedChanged += new EventHandler(cb_CheckedChanged); - didn't do anything
//cb.CheckedChanged += new EventHandler(this.cb_CheckedChanged); - didn't do anything
//cb.ViewStateMode = System.Web.UI.ViewStateMode.Enabled; - didn't do anything

cb.ToolTip = dr["Id"].ToString();
cb.ID = Guid.NewGuid().ToString();

Label lbl = new Label();
lbl.Text = dr["Id"].ToString();
lbl.AssociatedControlID = cb.ID;

dvCheckboxes.Controls.Add(cb);
dvCheckboxes.Controls.Add(lbl);
dvCheckboxes.Controls.Add(new LiteralControl("<br />"));

And the event:

void cb_CheckedChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write(((CheckBox)sender).ToolTip);
}

I've put a breakpoint in the CheckedChanged event, but it's never reached.

EDIT: I've tried putting the code in if(!IsPostBack), but no difference.




Aucun commentaire:

Enregistrer un commentaire