Click here to Skip to main content
15,067,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Master,content page concept.In master page two menus top,side menus are there.I wrote logic in Masterpage load event to bind Modules data dynamically to top menu.the side menu data will be binding dynamically when click on top menu item(binding pages data of respective module) When clicks on side menu item, it navigates to respective page and again logic in Master page load event is executing,at this time the dynamic data in the master page will be losting(top menu data binding again and side menu data losting).
How to preserve master page top,side menus with dynamic data when navigating to another page.


CODE:

MasterPage.aspx

XML
<form id="form1" runat="server">
    <div>
        <div id="header">
            <asp:Menu ID="navMenu" runat="server"
                RenderingMode="List" Orientation="Horizontal" OnMenuItemClick="navMenu_MenuItemClick">
            </asp:Menu>
        </div>

        <div id="nav">

            <asp:Menu ID="Menu1" runat="server"
                RenderingMode="List" Orientation="Vertical">
            </asp:Menu>
        </div>

        <div id="section">
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1"    runat="server">
            </asp:ContentPlaceHolder>
        </div>

        <div id="footer">
        </div>

    </div>
    </form>




MasterPage.aspx.cs

C#
public partial class MasterPage : System.Web.UI.MasterPage

 {

   protected void Page_Load(object sender, EventArgs e)

    {
        if (!IsPostBack)
        {
            BindData();
        }
    }


    private void BindData()
    {
        navMenu.Items.Clear();
        MenuItem mitem1 = new MenuItem();
        mitem1.Text = "HR";
        mitem1.Value = "0";
        MenuItem mitem2 = new MenuItem();
        mitem2.Text = "Leave";
        mitem2.Value = "1";
        navMenu.Items.Add(mitem1);
        navMenu.Items.Add(mitem2);
    }

    protected void navMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        Menu1.Items.Clear();
        switch (Convert.ToInt32(e.Item.Value))
        {
            case 0:
                MenuItem mitem1 = new MenuItem();
                mitem1.Text = "Department";
                mitem1.Value = Guid.NewGuid().ToString();
                mitem1.NavigateUrl = "Department.aspx";
                MenuItem mitem2 = new MenuItem();
                mitem2.Text = "JobTitle";
                mitem2.Value = Guid.NewGuid().ToString();
                mitem2.NavigateUrl = "JobTitle.aspx";
                MenuItem mitem3 = new MenuItem();
                mitem3.Text = "Employees";
                mitem3.Value = Guid.NewGuid().ToString();
                mitem3.NavigateUrl = "Employees.aspx";
                Menu1.Items.Add(mitem1);
                Menu1.Items.Add(mitem2);
                Menu1.Items.Add(mitem3);

                return;
            case 1:
                MenuItem mitem5 = new MenuItem();
                mitem5.Text = "Leave Request";
                mitem5.Value = Guid.NewGuid().ToString();
                mitem5.NavigateUrl = "LeaveRequest.aspx";
                MenuItem mitem6 = new MenuItem();
                mitem6.Text = "Approve Leave";
                mitem6.Value = Guid.NewGuid().ToString();
                mitem6.NavigateUrl = "ApproveLeave.aspx";

                Menu1.Items.Add(mitem6);
                Menu1.Items.Add(mitem5);

                return;

        }
    }
}





Thanks,
Srinivas
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900