我有一个母版页。我想向该页面运行时添加 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;
}
}