Raising Events on Loaded Custom Control

Hi,

How do I capture the event on a custom control which has been loaded dynamically.

My control has the following code,

public event EventHandler Filter;
private void OnFilter()
    {
        if (Filter != null)
            Filter(this, new EventArgs());
    }

...and in the button click event:

protected void btnRefreshGrid_Click(object sender, EventArgs e)
    {
        if (Filter != null) Filter(this, new EventArgs());
    }


The main page loads this control dynamically using:

Panel1.Controls.Add(MyControl);

So, how do I ensure that this loaded control has access to the OnFilter event in the control?

Thanks in advance,
R
LVL 4
rallsaldoAsked:
Who is Participating?
 
nouloukCommented:
Hi,
Panel1.Controls.Add(MyControl);
MyControl.Filter+=new EventHandler(MyControl_Filter);

protected void btnRefreshGrid_Click(object sender, EventArgs e)
    {
       OnFilter(this, new EventArgs());
    }

public event EventHandler Filter;
private void OnFilter(EventArgs e)
    {
        if (Filter != null)
            Filter(this, e);
    }

Hope this helps.
0
 
rallsaldoAuthor Commented:
Thanks, this does work but only where the control is loaded on the page load event. Is this right? Is it possible to load the custom control on some other event (like a button click) and then raise the event?

Say, MyControl has a button, as soon as the button is clicked the page refreshes and so the event is lost....

Thanks in advance for your help,
R
0
 
nouloukCommented:
OK, I think I understand what you need:
Inside your custom control, you have 1 button to refresh the grid.
1: you want to handle when the event is fired inside your webpage (this is done)
2: you want to refresh the grid even if the user doesn't click the refresh button of your custom control :
To do such a thing:
public void OnFilter(EventArgs e)    // modify to public
    {
        if (Filter != null)
            Filter(this, e);
    }

protected void btn1_Click(object sender, EventArgs e)
    {
       MyControl.OnFilter(new EventArgs());
    }

Regards.
0
 
nouloukCommented:
Or the method you use to refresh the grid inside your custom control.
0
 
rallsaldoAuthor Commented:
Hi thanks for your help. Its not quite what I am doing, I'll try to explain a bit more...

Say I have a Page (Default.aspx) with a Panel1 and a Button1. With the following code:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyControl mc = (MyControl)Page.LoadControl("MyControl.ascx");
        Panel1.Controls.Add(mc);
        mc.Filter += new EventHandler(MyControl_Filter);
    }
    protected void MyControl_Filter(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
}

and then a control called MyControl, with this code:

public partial class MyControl : System.Web.UI.UserControl
{
    public event EventHandler Filter;
    private void OnFilter()
    {
        if (Filter != null)
            Filter(this, new EventArgs());
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Filter != null) Filter(this, new EventArgs());
    }
}
When you click the button on the loaded control, the event MyControl_Filter is raised fine. However if I move the control loading code to the Button1_Click event on the page like so:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void MyControl_Filter(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyControl mc = (MyControl)Page.LoadControl("MyControl.ascx");
        Panel1.Controls.Add(mc);
        mc.Filter += new EventHandler(MyControl_Filter);

    }
}

So the control is loaded on the button click, when the control button is clicked the event doesn't raise, I guess because the first thing it does is submit the page, and then the control doesn't exist?

Sorry if this is obvious and I am being thick - thanks for your help so far!
R
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.