我有一个数据库中包含一堆不同的控件的表。在我的Page_Init方法中,我需要根据正在传入的Session变量加载适当的控件。是否有更好的方法来完成此操作,然后使用一大堆if..else语句?我有大约15到20个不同的场景,所以我不想写20个if..else语句。任何帮助是极大的赞赏!替代if..else语句
DataTable的题为 “值” 有三列:(ID,名称,描述):
ID | Name | Description
-------------------
1 | A | First
2 | B | Second
3 | C | Third
这里是我的代码:
ControlOne c1;
ControlTwo c2;
ControlThree c3;
protected void Page_Init(object sender, EventArgs e)
{
DataSet DS = Client.GetInformation(Session["Number"].ToString());
DataRow DR = DS.Tables["Value"].Rows[0];
if (DR["Name"].ToString() == "A" && DR["Description"].ToString() == "First")
{
c1 = (ControlOne)LoadControl("~/ControlOne.ascx");
panel1.Controls.Add(c1);
}
else if (DR["Name"].ToString() == "B" && DR["Description"].ToString() == "Second")
{
c2 = (ControlTwo)LoadControl("~/ControlTwo.ascx");
panel1.Controls.Add(c2);
}
else if (DR["Name"].ToString() == "C" && DR["Description"].ToString() == "Third")
{
c3 = (ControlThree)LoadControl("~/ControlThree.ascx");
panel1.Controls.Add(c3);
}
else if... //lists more scenarios here..
}
你的意思是一个switch语句? –
使用字符串映射到类型。 –
如何连接串并使用开关盒。 –