How can a control in an asp.net page tell its cont

2019-09-05 00:12发布

问题:

It is difficult to explain but here it goes

I have a custom control in my asp.net page, i have two files which i pass page by page to the control, the user does some editing on that file's page (loaded in the control) and when it reaches the end i want the control to some how let the page know that the end of the page has been reached, now load a new page in the control,

How should i achieve this and which is the best practice?

回答1:

You can bubble up the event from user control to the parent.

ParentAddUser.aspx

<uc1:AddUser ID="AddUser1" runat="Server" OnUserCreated="AddUser1_UserCreated"></uc1:AddUser>

ParentAddUser.aspx.cs

protected void AddUser1_UserCreated(object sender, CommandEventArgs e)
{
    // User was created successfully. Do Something here.
}

AddUser.ascx.cs

public event CommandEventHandler UserCreated;
protected void Button_Click(object sender, EventArgs e)
{
    // User was created successfully. Bubble up the event to parent. 
        UserCreated(this, new CommandEventArgs("UserId", userId.ToString()));

}