?
Solved

Raising Events on Loaded Custom Control

Posted on 2006-06-05
5
Medium Priority
?
642 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:rallsaldo
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
noulouk earned 1500 total points
ID: 16832659
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
 
LVL 4

Author Comment

by:rallsaldo
ID: 16835823
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
 
LVL 9

Expert Comment

by:noulouk
ID: 16837184
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
 
LVL 9

Expert Comment

by:noulouk
ID: 16837202
Or the method you use to refresh the grid inside your custom control.
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 16842249
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question