Dynamically wire up Button Click event in Composite Control

meepsittE

New Member
I have a custom composite control \[code\]NoteHeaderDiv\[/code\], containing a Button:\[code\]//NoteHeaderControl snippetpublic event EventHandler OnEditClick;protected void btnEdit_Click(object sender, EventArgs e){ if (OnEditClick != null) { OnEditClick(sender, e); }}public NoteHeaderControl(){ this.noteHeaderDiv = new HtmlGenericControl("div"); this.noteHeaderDiv.ID = "noteHeaderDiv"; this.noteHeaderDiv.Attributes.Add("class", "noteHeaderDiv"); this.imagePlaceHolder = new HtmlGenericControl("span"); this.imagePlaceHolder.ID = "imagePlaceHolder"; this.imagePlaceHolder.Attributes.Add("class", "noteCollapsed"); this.lblNoteDate = new Label(); this.lblNoteDate.ID = "lblNoteDate"; this.lblNoteDate.CssClass = "noteDate"; this.lblNoteTitle = new Label(); this.lblNoteTitle.ID = "lblNoteTitle"; this.lblNoteTitle.CssClass = "noteTitle"; this.lblNoteAuthorName = new Label(); this.lblNoteAuthorName.ID = "lblNoteTitle"; this.lblNoteAuthorName.CssClass = "noteAuthor"; this.editButton = new Button(); this.editButton.CssClass = "editNoteButton"; this.editButton.ToolTip = "Editovat"; this.editButton.ID = "editButton"; this.editButton.OnClientClick = "showEditDialog('editPerson');"; this.editButton.Click += btnEdit_Click; this.addTeamTaskButton = new HtmlGenericControl("button"); this.addTeamTaskButton.ID = "addTeamTaskButton"; this.addTeamTaskButton.Attributes.Add("class", "addTeamTaskButton"); this.addTeamTaskButton.Attributes.Add("onclick", "showDialog('editPerson');"); this.addTeamTaskButton.Attributes.Add("title", "Novy Team Task"); }#region protected override void CreateChildControls()protected override void CreateChildControls(){ base.CreateChildControls(); this.lblNoteDate.Text = String.Format("{0}.{1}.", this.noteDate.Day, this.noteDate.Month); this.lblNoteTitle.Text = noteTitle; this.lblNoteAuthorName.Text = noteAuthorName; //this.editButton.Click += btnEdit_Click; this.noteHeaderDiv.Controls.Add(imagePlaceHolder); this.noteHeaderDiv.Controls.Add(lblNoteDate); this.noteHeaderDiv.Controls.Add(lblNoteTitle); this.noteHeaderDiv.Controls.Add(lblNoteAuthorName); this.noteHeaderDiv.Controls.Add(editButton); this.noteHeaderDiv.Controls.Add(addTeamTaskButton); this.Controls.Add(noteHeaderDiv);}protected override void OnInit(EventArgs e){ CreateChildControls(); base.OnInit(e);}\[/code\]Then in \[code\]Page_Load\[/code\], I wire up the public event to another handler. Problem is, that \[code\]btnEdit_Click\[/code\] never fires. If I dynamically create a button in my \[code\]Page_Load\[/code\] and wire it up directly to the event handler defined in my \[code\]Page\[/code\], everything works as expected.Any suggestions about how to get it to work? I'm at a loss as to what I'm doing wrong.
 
Back
Top