How to raise an event from a dynamically generated user control

How do I create an event from a custom control that was created dynamically?

Here's the code I'm trying to use where Bind is the 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 am getting null object for IndDemographics. Can anyone point me to a complete sample code? Thanks in advance...

+1


source to share


3 answers


First of all, you need to make sure you have the event defined in your custom code.

eg:

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 that, you can bind to the event. Now, for your other problem, are you loading this control dynamically or declaring it on the ASPX side? If it's on the ASPX side, then you don't need LoadControl, since declaring the object as Runat = Server on the ASPX side creates an instance of the specified class.

If not, then you need to make sure you are using the virtual path for the location of the ASCX file. (in your example, you would use "~ / indDemographics.ascx" if the ASCX was in the root of the website). At this point, you will need to add it to the page (or placeholder or some other container object).

Regardless of how you instantiate the UserControl, you then bind the event handler to the event of the class instance. For example:



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

      

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

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

When you use LoadControl you instantiate indDemographics and assign it to UC. When you try to assign an event handler to the IndDemographics variable, it has never been created.

Ultimately, your code should look like this:

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

      

+3


source


I can see that this (IndDemographics) is declared but was never created, so I expect it to be null code with just this code.



+1


source


Thanks to Stephen for being 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 is important to use a common control in the indDemographics class. After that, everything else works fine.

+1


source







All Articles