Can you create an event in a user control, if so, how?
I have created a User Control containing a DatePicker and I want create an event in the user control linking to the DatePicker DateChanged e开发者_运维知识库vent. This custom user control will be used in an itemscontrol.
Yes. Add a public event to the control. And then add a method that looks for delegates attached to the event. If there are any delegates, raise the event. Here's an example:
In the User Control:
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);
}
}
Now, in the web form utilizing the user control:
<ppc:UserComments ID="UserComments_ObservationComments" runat="server"
OnCommentEditing="RefreshComments"
/>
Good luck!
精彩评论