Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加运行时菜单_C#_Asp.net - Fatal编程技术网

C# 添加运行时菜单

C# 添加运行时菜单,c#,asp.net,C#,Asp.net,我有一个母版页。我想向该页面运行时添加3个菜单项。 如何在运行时将父菜单和子菜单添加到母版页? 在前两个菜单项中,有两个子菜单项。我该怎么做 代码如下 public partial class MasterPage2 : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { LblDate.Text = DateTime.Today.ToString("dddd dd,M

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

代码如下

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

}

您可以在代码隐藏中动态向菜单添加节点:

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

@单击父菜单时,应显示ebad86子项。使用ur代码,子项作为父菜单添加。我希望子项显示在父菜单下。正在一次又一次地为ChildMenuItem分配新对象。错误的方法。可以尝试添加新实例,如ChildMenuItem1、ChildMenuItem2等,也可以这样添加:ParentMenuItem.ChildItems.Add(新建CreateMnuItem(“管理系统密码”)、“~/SearchPasswordInformation.aspx”和“);