0

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

}

0

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

@ ebad86 Child items should appear when i click on parent menu. With ur code child items are added as parent menu. i want child items to be display under parent menu..
ChildMenuItem is being assigned new object again and again. wrong approach. either try adding new instances like ChildMenuItem1, ChildMenuItem2 etc. or like this: ParentMenuItem.ChildItems.Add( new CreateMenuItem("Manage System's Password", "~/SearchPasswordInformation.aspx", ""));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.