I have an application the will load usercontrols dynamically depending on the user. You will see in the example below that I am casting each user control via switch/case statements. Is there a better way to do this? Reflection? (I must be able to add an event handler Bind in 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!