在Master Page中快速创建菜单

时间:2014-01-11 10:45:45

标签: asp.net master-pages

您好我有一个网站,其主菜单根据用户的角色填写 这需要在母版页中有很多菜单处理代码,事情正常 我很想知道,这种方法是否有任何性能下降 或者是否有更好的方式以这种方式放置菜单(我没有使用默认的asp.net Db和帐户等)这是我的代码:

DAO dao = new DAO();
protected void Page_Load(object sender, EventArgs e)
{ DispMenues(); }

protected void btnLogin_Click(object sender, EventArgs e)
{ /* Login Mechanism */ }

private void DispMenues()
{ // Menu is main menue on the page
    Menu.Items.Clear();
    MenuItem mnuHome = new MenuItem("&nbsp&nbspHome&nbsp&nbsp", "Home");
    mnuHome.NavigateUrl = "~/index.aspx";
    Menu.Items.Add(mnuHome);

    if (Session["urole"] != null)
        switch (Session["urole"].ToString())
        {
            case "admin":
                AdmMenu();
                break;
            case "supervisor":
                SupMenu();
                break;
            case "deo":
                DEOMenu();
                break;
            default:
                Response.Redirect("index.aspx");
                break;
        }

}

#region AllMenues

private void AdmMenu()
{
    DataFeeding(); EditData();
    Reports();
}
// For other users menu funcions are similar as above

// Actual Menu Functions
private void DataFeeding()
{
    // Main menu data feeding
    MenuItem mnuMainDataFeed = new MenuItem("&nbsp&nbspData Feeding&nbsp&nbsp", "DataFeeding");

    // Add first level menus under Data Feeding main menu
    MenuItem mnuMlkProd = new MenuItem("Milk Production", "MilkProduction");
    mnuMlkProd.NavigateUrl = "~/mprodentry.aspx";

    MenuItem mnuPedigree = new MenuItem("Pedigree", "Pedigree");
    mnuPedigree.NavigateUrl = "~/pedigentry.aspx";

    MenuItem mnuPedigreeExt = new MenuItem("External Animal", "External Animal");
    mnuPedigreeExt.NavigateUrl = "~/pedigextrnlentry.aspx";

    MenuItem mnuDisposalData = new MenuItem("Disposal", "Disposal");
    mnuDisposalData.NavigateUrl = "~/dispentry.aspx";

    mnuMainDataFeed.ChildItems.Add(mnuMlkProd);
    mnuMainDataFeed.ChildItems.Add(mnuPedigree);
    mnuMainDataFeed.ChildItems.Add(mnuPedigreeExt);

    // Add 2nd/3rd level menus under Data Feeding > Feed
    MenuItem mnuFeedingData = new MenuItem("Feeding", "Feeding");
    MenuItem mnuYoungAnimFeed = new MenuItem("Young Animal", "YoungAnimal");
    mnuYoungAnimFeed.NavigateUrl = "~/calfdngentry.aspx";
    mnuFeedingData.ChildItems.Add(mnuYoungAnimFeed);

    MenuItem mnuLactDryAnim = new MenuItem("Lactating/Dry Animal", "LactDryAnim");
    mnuLactDryAnim.NavigateUrl = "~/lctdryentry.aspx";
    mnuFeedingData.ChildItems.Add(mnuLactDryAnim);

    MenuItem mnuBullsFeed = new MenuItem("Bulls", "Bulls");
    mnuBullsFeed.NavigateUrl = "~/bullfdentry.aspx";
    mnuFeedingData.ChildItems.Add(mnuBullsFeed);

    mnuMainDataFeed.ChildItems.Add(mnuFeedingData);

    // Add 2nd/3rd level menus under Data Feeding > Breeding and Reproduction
    MenuItem mnuBreedReprod = new MenuItem("Breeding & Reproduction", "BreedReprod");

    MenuItem mnuCalvingRecord = new MenuItem("Calving Record", "CalvingRecord");
    mnuCalvingRecord.NavigateUrl = "~/calvngentry.aspx";
    mnuBreedReprod.ChildItems.Add(mnuCalvingRecord);

    MenuItem mnuServiceRecord = new MenuItem("Service Record", "ServiceRecord");
    mnuServiceRecord.NavigateUrl = "~/seviceentry.aspx";
    mnuBreedReprod.ChildItems.Add(mnuServiceRecord);

    MenuItem mnuServicePos = new MenuItem("Service Position", "Service Position");
    mnuServicePos.NavigateUrl = "~/servposentry.aspx";
    mnuBreedReprod.ChildItems.Add(mnuServicePos);

    mnuMainDataFeed.ChildItems.Add(mnuBreedReprod);


    // Add 2nd/3rd level menus under Data Feeding > Diseases
    MenuItem mnuDiseases = new MenuItem("Diseases", "Diseases");
    MenuItem mnuDiseaseData = new MenuItem("Disease Data", "Disease Data");
    mnuDiseaseData.NavigateUrl = "~/disentry.aspx";
    mnuDiseases.ChildItems.Add(mnuDiseaseData);

    MenuItem mnuTreatData = new MenuItem("Treatment Data", "Treatment Data");
    mnuTreatData.NavigateUrl = "~/treatmntentry.aspx";
    mnuDiseases.ChildItems.Add(mnuTreatData);

    mnuMainDataFeed.ChildItems.Add(mnuDiseases);

    // Add 2nd/3rd level menus under Data Feeding > Inventory
    MenuItem mnuInventory = new MenuItem("Inventory", "Inventory");

    MenuItem mnuStock = new MenuItem("Stock", "Stock");
    mnuStock.NavigateUrl = "~/inventstckentry.aspx";
    mnuInventory.ChildItems.Add(mnuStock);

    MenuItem mnuDeadStock = new MenuItem("Dead Stock", "DeadStock");
    mnuDeadStock.NavigateUrl = "~/deadstckentry.aspx";
    mnuInventory.ChildItems.Add(mnuDeadStock);

    mnuMainDataFeed.ChildItems.Add(mnuInventory);

    // Add 2nd/3rd level menus under Data Feeding > Land
    MenuItem mnuLand = new MenuItem("Land", "Land");

    MenuItem mnuLandRecord = new MenuItem("Land Record", "LandRecord");
    mnuLandRecord.NavigateUrl = "~/landrcrdentry.aspx";
    mnuLand.ChildItems.Add(mnuLandRecord);

    MenuItem mnuCultivatatedLand = new MenuItem("Cultivated", "Cultivated");
    mnuCultivatatedLand.NavigateUrl = "~/landcultvtdentry.aspx";
    mnuLand.ChildItems.Add(mnuCultivatatedLand);

    MenuItem mnuCultivationYears = new MenuItem("Cultivation Years", "CultivationYears");
    mnuCultivationYears.NavigateUrl = "~/cultyrsentry.aspx";
    mnuLand.ChildItems.Add(mnuCultivationYears);

    mnuMainDataFeed.ChildItems.Add(mnuLand);

    MenuItem mnuMilkCons = new MenuItem("Milk Consumption", "MilkConsumption");
    mnuMilkCons.NavigateUrl = "~/milkconsmpentry.aspx";
    mnuMainDataFeed.ChildItems.Add(mnuMilkCons);


    // Add last main menu under Feed data menu
    mnuMainDataFeed.ChildItems.Add(mnuDisposalData);

    // Add feeding data main menu to menu bar
    Menu.Items.Add(mnuMainDataFeed);
}
private void EditData()
{
    // Main menu Edit Data
    MenuItem mnuMainEditData = new MenuItem("&nbsp&nbspEdit Data&nbsp&nbsp", "EditData");

    // Add first level menus under Data Feeding main menu
    MenuItem mnuEdtMlkProd = new MenuItem("Milk Production", "MilkProduction");
    mnuEdtMlkProd.NavigateUrl = "~/editmprod.aspx";

    MenuItem mnuEdtPedigree = new MenuItem("Pedigree", "Pedigree");
    mnuEdtPedigree.NavigateUrl = "~/editpedigdata.aspx";

    mnuMainEditData.ChildItems.Add(mnuEdtMlkProd);
    mnuMainEditData.ChildItems.Add(mnuEdtPedigree);



    // Add 2nd/3rd level menus under Data Feeding > Feed
    MenuItem mnuEdtFeedingData = new MenuItem("Feeding", "Feeding");
    MenuItem mnuEdtYoungAnimFeed = new MenuItem("Young Animal", "YoungAnimal");
    mnuEdtYoungAnimFeed.NavigateUrl = "~/editclfedng.aspx";
    mnuEdtFeedingData.ChildItems.Add(mnuEdtYoungAnimFeed);

    MenuItem mnuEdtLactDryAnim = new MenuItem("Lactating/Dry Animal", "LactDryAnim");
    mnuEdtLactDryAnim.NavigateUrl = "~/editdrylact.aspx";
    mnuEdtFeedingData.ChildItems.Add(mnuEdtLactDryAnim);

    MenuItem mnuEdtBullsFeed = new MenuItem("Bulls", "Bulls");
    mnuEdtBullsFeed.NavigateUrl = "~/editbulsfd.aspx";
    mnuEdtFeedingData.ChildItems.Add(mnuEdtBullsFeed);

    mnuMainEditData.ChildItems.Add(mnuEdtFeedingData);


    // Add 2nd/3rd level menus under Data Feeding > Breeding and Reproduction
    MenuItem mnuEdtBreedReprod = new MenuItem("Breeding & Reproduction", "BreedReprod");

    MenuItem mnuEdtCalvingRecord = new MenuItem("Calving Record", "CalvingRecord");
    mnuEdtCalvingRecord.NavigateUrl = "~/editcvngrcrd.aspx";
    mnuEdtBreedReprod.ChildItems.Add(mnuEdtCalvingRecord);

    MenuItem mnuEdtServiceRecord = new MenuItem("Service Record", "ServiceRecord");
    mnuEdtServiceRecord.NavigateUrl = "~/editsrvcrcrd.aspx";
    mnuEdtBreedReprod.ChildItems.Add(mnuEdtServiceRecord);

    MenuItem mnuEdtServicePos = new MenuItem("Service Position", "Service Position");
    mnuEdtServicePos.NavigateUrl = "~/editservpos.aspx";
    mnuEdtBreedReprod.ChildItems.Add(mnuEdtServicePos);

    mnuMainEditData.ChildItems.Add(mnuEdtBreedReprod);


    // Add 2nd/3rd level menus under Edit Data > Feeding > Diseases
    MenuItem mnuEdtDiseases = new MenuItem("Diseases", "Diseases");

    MenuItem mnuEdtDiseaseData = new MenuItem("Disease Data", "Disease Data");
    mnuEdtDiseaseData.NavigateUrl = "~/editdis.aspx";
    mnuEdtDiseases.ChildItems.Add(mnuEdtDiseaseData);

    MenuItem mnuEdtTreatData = new MenuItem("Treatment Data", "Treatment Data");
    mnuEdtTreatData.NavigateUrl = "~/edittrtmnt.aspx";
    mnuEdtDiseases.ChildItems.Add(mnuEdtTreatData);

    mnuMainEditData.ChildItems.Add(mnuEdtDiseases);

    // Add 2nd/3rd level menus under Edit Data > Inventory
    MenuItem mnuEdtStock = new MenuItem("Inventory", "Inventory");

    MenuItem mnuEdtOfficeStock = new MenuItem("Stock", "Stock");
    mnuEdtOfficeStock.NavigateUrl = "~/editinvntstck.aspx";
    mnuEdtStock.ChildItems.Add(mnuEdtOfficeStock);

    MenuItem mnuDeadStock = new MenuItem("Dead Stock", "DeadStock");
    mnuDeadStock.NavigateUrl = "~/editdeadstck.aspx";
    mnuEdtStock.ChildItems.Add(mnuDeadStock);

    mnuMainEditData.ChildItems.Add(mnuEdtStock);

    // Add 2nd/3rd level menus under Data Feeding > Land
    MenuItem mnuEdtLand = new MenuItem("Land", "Land");

    MenuItem mnuEdtLandRecord = new MenuItem("Land Record", "LandRecord");
    mnuEdtLandRecord.NavigateUrl = "~/editlandrcrd.aspx";
    mnuEdtLand.ChildItems.Add(mnuEdtLandRecord);

    MenuItem mnuEdtCultand = new MenuItem("Cultivated", "Cultivated");
    mnuEdtCultand.NavigateUrl = "~/editlandcultvtd.aspx";
    mnuEdtLand.ChildItems.Add(mnuEdtCultand);

    mnuMainEditData.ChildItems.Add(mnuEdtLand);

    // Add menu under Edit Data > Disposal
    MenuItem mnuEdtDispData = new MenuItem("Disposal", "Disposal");
    mnuEdtDispData.NavigateUrl = "~/editdisp.aspx";
    mnuMainEditData.ChildItems.Add(mnuEdtDispData);

    // Add edit data main menu to menu bar
    Menu.Items.Add(mnuMainEditData);
}
private void Reports()
{
    MenuItem mnuReportsMain = new MenuItem("&nbsp&nbspView Report&nbsp&nbsp", "ViewReport");
    mnuReportsMain.NavigateUrl = "~/reportoptions.aspx";
    // Add View Reports main menu
    Menu.Items.Add(mnuReportsMain);
}

#endregion

1 个答案:

答案 0 :(得分:0)

这很好。
如果我是你,我会在你的商品名称中使用css而不是“& nbsp” 此外,您可以使用带有... 5个参数(我认为)的MenuItem构造函数,包括NavigateUrl 或者你可以简单地使用默认构造函数并逐个初始化属性。

编辑:哦,另一件事......最好在菜单控件 DataBound事件中创建项目,你应该考虑使用SiteMap