How do I determine the type of dynamically loaded user control?

I have an application that will dynamically load custom controllers based on the user. In the example below, you will see that I am executing control of each user with switch / case statements. Is there a better way to do this? Reflection? (I should be able to add a Bind event handler to each control.)

override protected void OnInit(EventArgs e)
{
    cc2007.IndividualPageSequenceCollection pages = new IndividualPageSequenceCollection().Load();
    pages.Sort("displayIndex", true);
    foreach (IndividualPageSequence page in pages)
    {
        Control uc = Page.LoadControl(page.PageName);
        View view = new View();
        int viewNumber = Convert.ToInt32(page.DisplayIndex) -1;

        switch(page.PageName)
        {
            case "indStart.ascx":
                IndStart = (indStart) uc;
                IndStart.Bind += new EventHandler(test_handler);
                view.Controls.Add(IndStart);
                MultiView1.Views.AddAt(viewNumber, view);
                break;

            case "indDemographics.ascx":
                IndDemographics = (indDemographics)uc;
                IndDemographics.Bind += new EventHandler(test_handler);
                view.Controls.Add(IndDemographics);
                MultiView1.Views.AddAt(viewNumber, view);
                break;

            case "indAssetSurvey.ascx":
                IndAssetSurvey = (indAssetSurvey)uc;
                IndAssetSurvey.Bind += new EventHandler(test_handler);
                view.Controls.Add(IndAssetSurvey);
                MultiView1.Views.AddAt(viewNumber, view);
                break;
        }

    }
    base.OnInit(e);
}

      

Thanks in advance!

+1


source to share


4 answers


I don't see any control class in your code. You perform exactly the same operations and look like all custom controls inherit from Control.

If the only quirk has to do with event binding (for example, the Control class does not have a Bind event), then you better consider refactoring your code so that you endow all your custom elements like this: Control -> MyBaseControl (put the event here) -> YouControl.



If you can't control the source of the controls, then Jeroen's suggestion should work.

+1


source


What about:



Type t = uc.GetType ();
EventInfo evtInfo = t.GetEvent ("Bind");
evtInfo.AddEventHandler (this, new EventHandler (test_handler));


I have not tested this code, but it should be something like this.

+1


source


How do I define an interface with a Bind event and implement its controls?

public interface IBindable
{
  event EventHandler Bind;
}

      

Then:

foreach (IndividualPageSequence page in pages)
{
  IBindable uc = Page.LoadControl(page.PageName) as IBindable;
  if( uc != null )
  {
    uc.Bind += new EventHandler(test_handler);
    View view = new View();
    view.Controls.Add(page);
    int viewNumber = Convert.ToInt32(page.DisplayIndex) -1;
    MultiView1.Views.AddAt(viewNumber, view);
  }
}

      

+1


source


You can try TypeOf (in C #) or work in uc.GetType ().

0


source







All Articles