你能在用户控件中创建一个事件,如果是,怎么做?

时间:2010-12-01 13:46:35

标签: silverlight-4.0

我创建了一个包含DatePicker的用户控件,我希望在用户控件中创建一个链接到DatePicker DateChanged事件的事件。此自定义用户控件将用于itemscontrol。

1 个答案:

答案 0 :(得分:0)

是。将一个公共事件添加到控件。然后添加一个方法来查找附加到事件的委托。如果有任何代表,请举起活动。这是一个例子:

在用户控件中:

public partial class Controls_UserComments : System.Web.UI.UserControl
{
    // the event delegates may listen for
    public event EventHandler CommentEditing;

    protected void Page_Load(object sender, EventArgs e)
    {
    // handle an event in the user control and bubble the event up to any delegates
        GridView_Comments.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView_Comments_RowCancelingEdit);
    }

    void GridView_Comments_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView_Comments.EditIndex = -1;
        GridView_Comments.DataBind();

        // raise the event for attached delegates
        if (CommentEditing != null)
            CommentEditing(this, EventArgs.Empty);
    }
}

现在,在使用用户控件的网络表单中:

<ppc:UserComments ID="UserComments_ObservationComments" runat="server"
    OnCommentEditing="RefreshComments"
    />
祝你好运!