我有一个自定义复合控件NoteHeaderDiv
,包含一个按钮:
//NoteHeaderControl snippet
public 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", "Nový 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);
}
然后在 中Page_Load
,我将公共事件连接到另一个处理程序。问题是,它btnEdit_Click
永远不会触发。如果我在 my 中动态创建一个按钮Page_Load
并将其直接连接到 my 中定义的事件处理程序Page
,那么一切都会按预期工作。
关于如何让它工作的任何建议?我不知道我做错了什么。