动态连接Composite Control中的Button Click事件

时间:2012-12-07 13:05:11

标签: c# asp.net button composite-controls

我有一个自定义复合控件NoteHeaderDiv,其中包含一个Button:

//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永远不会发生。如果我在Page_Load中动态创建一个按钮并将其直接连接到我Page中定义的事件处理程序,那么一切都按预期工作。

有关如何使其发挥作用的任何建议?我不知道自己做错了什么。

0 个答案:

没有答案