添加运行时菜单

时间:2012-06-20 12:17:15

标签: c# asp.net

我有一个主页面。我想向该页面运行时添加3个菜单项。 如何在运行时将父菜单和子菜单添加到母版页? 在前2个菜单项中,有2个子菜单​​项。我怎么能这样做?

代码如下:

    public partial class MasterPage2 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
    LblDate.Text = DateTime.Today.ToString("dddd dd,MMM yyyy");
    lblusername.Text = Session["username"].ToString();
    if (Session["role"].ToString() == "1")
    {
        //Menu1.Items.Add(new MenuItem("System Information", "1", "", "~/home.aspx"));
        //Menu1.Items.Add(new MenuItem("Administration", "2", "", "~/home.aspx"));
        //Menu1.Items.Add(new MenuItem("Signout", "3", "", "~/Login.aspx"));

        //Menu1.FindItem("1").ChildItems.Add(new MenuItem("Search System Information", "", "", "~/SearchSystemInformation.aspx"));
        //Menu1.FindItem("1").ChildItems.Add(new MenuItem("Request New System", "", "", "~/RequestNewSystem.aspx"));

        //Menu1.FindItem("2").ChildItems.Add(new MenuItem("Manage System's Password", "", "", "~/SearchPasswordInformation.aspx"));
        //Menu1.FindItem("2").ChildItems.Add(new MenuItem("Manage Administrators", "", "", "~/ManageAdmins.aspx"));

        MenuItem ParentMenuItem = null;
        MenuItem ChildMenuItem = null;

        ParentMenuItem = CreateMenuItem("System Information", "~/home.aspx", "");

        ChildMenuItem = CreateMenuItem("Search System Information", "~/SearchSystemInformation.aspx", "");

        ParentMenuItem.ChildItems.Add(ChildMenuItem);

        ChildMenuItem = CreateMenuItem("Request New System", "~/RequestNewSystem.aspx", "");

        ParentMenuItem.ChildItems.Add(ChildMenuItem);

        Menu1.Items.Add(ParentMenuItem);

        ParentMenuItem = CreateMenuItem("Administration", "~/home.aspx", "");

        ChildMenuItem = CreateMenuItem("Manage System's Password", "~/SearchPasswordInformation.aspx", "");

        ParentMenuItem.ChildItems.Add(ChildMenuItem);

        ChildMenuItem = CreateMenuItem("Manage Administrators", "~/ManageAdmins.aspx", "");

        ParentMenuItem.ChildItems.Add(ChildMenuItem);

        Menu1.Items.Add(ParentMenuItem);

        ParentMenuItem = CreateMenuItem("Signout", "~/Login.aspx", "");
        Menu1.Items.Add(ParentMenuItem);

        //MenuItem mnuSystemInfo = new MenuItem();
        //mnuSystemInfo.NavigateUrl = "~/Home.aspx";
        //mnuSystemInfo.Text = "System Information";
        ////Menu1.Items.Add(mnuSystemInfo);

        //MenuItem mnuSearchSystemInfo = new MenuItem();
        //mnuSearchSystemInfo.NavigateUrl = "~/SearchSystemInformation.aspx";
        //mnuSearchSystemInfo.Text = "Search System Information";
        //mnuSystemInfo.ChildItems.Add(mnuSearchSystemInfo);
        //Menu1.Items.Add(mnuSystemInfo);
        //Menu1.Items.Add(mnuSearchSystemInfo);




    }
    else if(Session["role"].ToString()=="2")
    {
        //Menu1.Items.Clear();
        //Menu1.Items.Add(new MenuItem("System Information", "1", "", ""));
        //Menu1.Items.Add(new MenuItem("Signout", "3", "", ""));

        //Menu1.FindItem("1").ChildItems.Add(new MenuItem("Search System Information", "", "", "~/SearchSystemInformation.aspx"));
        //Menu1.FindItem("1").ChildItems.Add(new MenuItem("New System Request", "", "", "~/RequestNewSystem.aspx"));
    }
}

MenuItem CreateMenuItem(String text, String url, String toolTip)
{
    // Create a new MenuItem object.
    MenuItem menuItem = new MenuItem();

    menuItem.Text = text;
    menuItem.NavigateUrl = url;
    menuItem.ToolTip = toolTip;
    return menuItem;
}

}

1 个答案:

答案 0 :(得分:2)

您可以在Code behind中动态添加节点到菜单:

MenuItem mnuTest = new MenuItem();
mnuTest.NavigateUrl = "";
mnuTest.Text = "Test";
Menu1.Items.Add(mnuTest); 

您可以将子节点添加到菜单中:

MenuItem mnuTest = new MenuItem(); 
mnuTest.NavigateUrl = ""; 
mnuTest.Text = "Test"; 


MenuItem mnuTestChild = new MenuItem(); 
mnuTestChild.NavigateUrl = ""; 
mnuTestChild.Text = "Child Test"; 
mnuTest.ChildItems.Add(mnuTestChild); 
Menu1.Items.Add(mnuTestChild);