views:

2401

answers:

3

How do I raise an event from a user control that was created dynamically?

Here's the code that I'm trying where Bind is a public EventHandler

protected indDemographics IndDemographics;
protected UserControl uc;
override protected void OnInit(EventArgs e)
{
    uc = (UserControl)LoadControl("indDemographics.ascx");
    IndDemographics.Bind += new EventHandler(test_handler);
    base.OnInit(e);
}

I get a null object for IndDemographics. Can anyone point me to a complete code sample? Thanks in advance...

+1  A: 

I see it (IndDemographics) declared but never actually created, so I'd expect it to be null with just this code.

ctacke
+1  A: 

First off, you'll need to make sure that you have the event defined in your usercontrol's code.

for example:

public class MyUserControl
  Inherits UserControl

  Public Event Bind(sender as object, e as EventArgs)

  public sub SomeFunction()
     RaiseEvent Bind(me, new EventArgS())
  End Sub
End Class

After this, then you can bind to the Event. Now,for your other issue, are you loading this control dynamically or is it declared on your ASPX side? If it's on your ASPX side, then you don't need the LoadControl, as declaring an object as Runat=Server on the ASPX side instantiates an instance of said class.

If not, then you'll need to make sure you're using the Virtual Path for the location of the ASCX file. (in your example, you'd use "~/indDemographics.ascx" if the ASCX was at the root of the website). At this point you'd need to add it to the page (or a placeholder or some other container object).

Regardless, of which way you instantiate an instance of the UserControl, you then associate the Event Handler to the Event of the instance of the class. For example:

Dim btn As New Button;
AddHandler btn.Click, AddressOf MyButtonClickEventHandler

Now, for the reason that you're getting a NULL reference in the example code.

When you use the LoadControl reference, then the instance of your object is in the UC variable. In the example, you declare two objects, UC as a type of UserControl and indDemographics as a type of indDemographics.

When you use the LoadControl, you're instantiating an instance of indDemographics and assigning it to UC. When you try to assign the event handler to the IndDemographics variable, it has never actually been instantiated.

Ultimately, your code should look more along these lines:

protected indDemographics IndDemographics;
override protected void OnInit(EventArgs e)
{
    indDemographics = LoadControl("~/indDemographics.ascx");
    IndDemographics.Bind += new EventHandler(test_handler);
    base.OnInit(e);
}
Stephen Wrighton
A: 

Thanks to Stephen for getting me on the right track. Here's the final working code in C#:

protected indDemographics IndDemo;
override protected void OnInit(EventArgs e)
{
    Control c = LoadControl("~/indDemographics.ascx");
    IndDemo = (indDemographics) c;
    IndDemo.Bind += new EventHandler(test_handler);
    place1.Controls.Add(IndDemo);
    base.OnInit(e);
}

It's important to cast the generic control into the indDemographics class. After that everything else works fine.

Geri Langlois