添加click if语句

时间:2014-11-21 14:12:56

标签: c# asp.net

有没有更好的方式通过addClick传递此信息(理想情况下,我根本不想要这个,我希望它能自动通过)?

public void addClick(object sender, EventArgs e)
{
    if ((string) HttpContext.Current.Session["whichMenu"] == "systemDateFormats")
    {
        WorldViewNet.system.DateFormats dateformats = new WorldViewNet.system.DateFormats();
        dateformats.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingLabels")
    {
        WorldViewNet.programming.Labels labels = new WorldViewNet.programming.Labels();
        labels.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingPLUSearch")
    {
        WorldViewNet.programming.PLUSearch pluSearch = new WorldViewNet.programming.PLUSearch();
        pluSearch.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingServings")
    {
        WorldViewNet.programming.Servings servings = new WorldViewNet.programming.Servings();
        servings.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingShops")
    {
        WorldViewNet.programming.Shops shops = new WorldViewNet.programming.Shops();
        shops.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingTextsSearch")
    {
        WorldViewNet.programming.TextsSearch textsSearch = new WorldViewNet.programming.TextsSearch();
        textsSearch.addClick();
    }
    else if ((string) HttpContext.Current.Session["whichMenu"] == "systemTemplates")
    {
        WorldViewNet.system.Templates templates = new WorldViewNet.system.Templates();
        templates.addClick();
    }
}

如果有人有任何建议,那将有助于我,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我在下面的模型可能对您的代码有用:

public void addClick(object sender, EventArgs e)
{
    object control;
    string opt = (string) HttpContext.Current.Session["whichMenu"];

    switch (opt)
    {
        case "systemDateFormats": control = new WorldViewNet.system.DateFormats();
            break;
        case "programmingLabels": control = new WorldViewNet.programming.Labels();
            break;
        case "programmingPLUSearch": control = new WorldViewNet.programming.PLUSearch();
            break;
        case "programmingServings": control = new WorldViewNet.programming.Servings();
            break;
        case "programmingShops": control = new WorldViewNet.programming.Shops();
            break;
        case "programmingTextsSearch": control = new WorldViewNet.programming.TextsSearch();
            break;
        case "systemTemplates": control = new WorldViewNet.system.Templates();
            break;
        default: new WorldViewNet.system.DefaultType();
    }

    ((dynamic)control).addClick();
}