Abu
Abu

Reputation: 39

Adding runtime menu

I have a one master Page. I want to add 3 Menu items to that page runtime. How to add Parent menus and Child menus at runtime to master page? In first 2 Menu items, there are 2 sub menu items. How can I do this?

The code is as follwos.

    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;
}

}

Upvotes: 0

Views: 1804

Answers (1)

Ebad Masood
Ebad Masood

Reputation: 2379

You can add Nodes to Menu dynamically in Code behind:

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

You can add Child nodes to Menu as:

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);

Upvotes: 2

Related Questions