如何使用动态母版页为每个用户制作自定义菜单?

时间:2014-03-25 20:27:18

标签: asp.net master-pages

我的问题几乎与this question

相同

但我想使用动态母版页,是否可能?

我想动态地为每个用户的菜单应用限制。 我是asp.net的新手,我所知道的是有一种方法可以动态地使用母版页,但我不知道怎么做!

更新:

简单地说,我想根据用户的身份更改我的菜单项,我想知道是否可以通过动态更改母版页(保存菜单)的内容来实现这一点。

顺便说一下,我用HTML做了一切。 到目前为止我还没有使用过asp标签。就像我之前说过的,我是asp.net的新手。

1 个答案:

答案 0 :(得分:0)

我在这里做了同样的事情是我可以提供给你的代码(主菜单控件的名称 是:菜单,所以这是主页面代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            DispMenues();
        }


 private void DispMenues()
        {
            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 "supadmin":
                        SupAdmMenu();
                        break;
                    case "controller":
                        ContMenu();
                        break;
                    case "admin":
                        AdmMenu();
                        break;
                    case "supervisor":
                        SupMenu();
                        break;
                    case "deo":
                        DEOMenu();
                        break;
                    default:
                        Response.Redirect("index.aspx");
                        break;
                }

            MenuItem mnuTools = new MenuItem("&nbsp&nbspTools&nbsp&nbsp", "Tools");
            mnuTools.NavigateUrl = "~/tools.aspx";
            Menu.Items.Add(mnuTools);
        }

        #region AllMenues
        private void SupAdmMenu()
        {
            DataFeeding(); Manage();
            EditData(); Reports();
        }

        private void ContMenu()
        {
            DataFeeding(); Manage();
            EditData(); Reports();
        }

        private void AdmMenu()
        {
            DataFeeding(); EditData();
            Reports();
        }

        private void SupMenu()
        {
            DataFeeding(); EditData();
            Reports();
        }

        private void DEOMenu()
        {
            DataFeeding(); EditData();
        }
        // 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 Manage()
        {
            MenuItem mnuMainMngAll = new MenuItem("&nbsp&nbspManage&nbsp&nbsp", "Manage");
            string urole = Session["urole"].ToString();

            MenuItem mnuManageLoc = new MenuItem("Manage Location", "ManageLocation");
            // Shared inside Manage Locations
            MenuItem mnuMngProv = new MenuItem("Province", "Province");
            mnuMngProv.NavigateUrl = "~/manageprov.aspx";
            mnuManageLoc.ChildItems.Add(mnuMngProv);

            MenuItem mnuStations = new MenuItem("Stations", "Stations");
            mnuStations.NavigateUrl = "managest.aspx";
            mnuManageLoc.ChildItems.Add(mnuStations);

            MenuItem mnuMngNewAccReqs = new MenuItem("New Accounts", "NewAccounts");
            mnuMngNewAccReqs.NavigateUrl = "approveacc.aspx";
            mnuMainMngAll.ChildItems.Add(mnuMngNewAccReqs);

            MenuItem mnuMngPasswords = new MenuItem("Manage Users", "ManageUsers");
            mnuMngPasswords.NavigateUrl = "~/manageusers.aspx";
            mnuMainMngAll.ChildItems.Add(mnuMngPasswords);

            // Only supervisor has access to Manage Country menu
            if (urole.Equals("supadmin"))
            {
                MenuItem mnuMngCountry = new MenuItem("Country", "Country");
                mnuMngCountry.NavigateUrl = "~/SupAdmSection/managecountry.aspx";
                mnuManageLoc.ChildItems.Add(mnuMngCountry);
            }

            mnuMainMngAll.ChildItems.Add(mnuManageLoc);

            MenuItem mngAnimStatus = new MenuItem("Animal Status", "AnimStatus");
            mngAnimStatus.NavigateUrl = "manageanimst.aspx";
            mnuMainMngAll.ChildItems.Add(mngAnimStatus);

            MenuItem mngBulServStatus = new MenuItem("Bull Service", "BullService");
            mngBulServStatus.NavigateUrl = "managebullsrvst.aspx";
            mnuMainMngAll.ChildItems.Add(mngBulServStatus);

            MenuItem mngCalfStatus = new MenuItem("Calf Status", "CalfStatus");
            mngCalfStatus.NavigateUrl = "managecalfst.aspx";
            mnuMainMngAll.ChildItems.Add(mngCalfStatus);

            MenuItem mngDisCondition = new MenuItem("Disease Condition", "DiseaseCondition");
            mngDisCondition.NavigateUrl = "managediscond.aspx";
            mnuMainMngAll.ChildItems.Add(mngDisCondition);

            MenuItem mngDisName = new MenuItem("Disease Name", "DiseaseName");
            mngDisName.NavigateUrl = "managedisnam.aspx";
            mnuMainMngAll.ChildItems.Add(mngDisName);

            MenuItem mngDispReason = new MenuItem("Disposal Reason", "DisposalReason");
            mngDispReason.NavigateUrl = "managedisprsn.aspx";
            mnuMainMngAll.ChildItems.Add(mngDispReason);

            MenuItem mngFertPos = new MenuItem("Fertility Position", "FertilityPosition");
            mngFertPos.NavigateUrl = "managefertpos.aspx";
            mnuMainMngAll.ChildItems.Add(mngFertPos);

            MenuItem mngMlkSaleRate = new MenuItem("Milk Sale Rate", "MilkSaleRate");
            mngMlkSaleRate.NavigateUrl = "managemlkslrt.aspx";
            mnuMainMngAll.ChildItems.Add(mngMlkSaleRate);

            MenuItem mngBreed = new MenuItem("Animal Breed", "Animal Breed");
            mngBreed.NavigateUrl = "manageanimbrd.aspx";
            mnuMainMngAll.ChildItems.Add(mngBreed);

            MenuItem mngAnimTerm = new MenuItem("Animal Term Name", "ManageTermName");
            mngAnimTerm.NavigateUrl = "manageanmtrm.aspx";
            mnuMainMngAll.ChildItems.Add(mngAnimTerm);


            Menu.Items.Add(mnuMainMngAll);
        }
        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
    }