我有一個應用程序將根據用戶動態加載usercontrols。您將在下面的示例中看到,我通過switch/case語句來投射每個用戶控件。有一個更好的方法嗎?反射? (我必須能夠在每個控件中添加事件處理程序綁定。)如何確定動態加載的usercontrol的類型?
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);
}
在此先感謝!