Subscribing to an event of a ascx of another ascx

2019-09-08 06:23发布

问题:

I'm trying to subscribe to the the save button event of a user control that is launched in a separate radwindow from the calling parent. but I am getting object not initialized error, I know why but what am I missing?

Update: I found my error but it appears that if (this.SaveEvent!= null) in the ControlBase is always null

Parent Control Code:

public partial class myControl : ControlBase
{
    private myChildControl __myChildControl;

    private void myControl_PreRender(object sender, EventArgs e)
    {
        // error occurs here
        //this.__myChildControl.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
        // found my error 
        this.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
    }

    private void __myChildControl _SaveEvent(object sender, CustomEventArgs e)
    {
         this.Label1.Text = e.CustomEventArg1.ToString();
         this.Label2.Text = e.CustomEventArg2.ToString();
    }
}

Child Control Launched in RadWindow:

public partial class myChildControl : ControlBase
{
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        CustomEventArgs _cea = new CustomEventArgs {CustomEventArg1 = 123, CustomEventArg2 = 12};
        callBaseMethod(_cea);
    }
}

ControlBase Code:

public class ControlBase : UserControl
{
    public event CustomEventHandler SaveEvent;
    public delegate void CustomEventHandler(object sender, CustomEventArgs e);

    internal void callBaseMethod(CustomEventArgs cea)
    {
        if (this.SaveEvent!= null)
        {
            this.SaveEvent(this, cea);
        }
    }
}

CustomEventArgs class:

public class CustomEventArgs : EventArgs
{
    public int CustomEventArgs1 { get; set; }
    public int CustomEventArgs2 { get; set; }

}

回答1:

This isn't possible in codebehind: the RadWindow presents a separate aspx/ascx page altogether that is linked to the main page through javascript alone.

What you need to do is handle the RadWindow OnClientClose event in javascript, then fire something in the parent page that performs the appropriate tasks.