Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am creating a master page which includes menus as well as submenu according to the users access roles. i want to create it on runtime using for loop. please can anyone help me.

What I have tried:

C#
 if (Session["accesscode"].ToString() == "15")   
                {
                    usermgmt.Visible = true;
                    dispp.Visible = true;

                }
                else
                {
                    usermgmt.Visible = false;
                    dispp.Visible = false;
  
                }
                 string strParam;

            String strConnString = ConfigurationManager.ConnectionStrings["ROMAConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();

            if (!IsPostBack)
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select distinct a.MENU_ID,A.MENU_NAME,d.SUBMENU_ID, d.submenu_name from Menu_Master a , ROMA..USER_MASTER b , SubmenuRec c , Submenu D, Menu_IDCODE e where a.menu_id=c.menu_id  and b.accessrights = e.accessrights and c.submenu_id= d.submenu_id and b.username= ('" + strParam + "')";
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(ds);
                 
                for (int j = 0; j <= ds.Tables[0].Rows.Count - 1; j++)
                {

                    for (int i = 0; i <= Rows.Count - 1; i++)
                    {
                           
 /*MenuItem mnuTestChild = new MenuItem();
mnuTestChild.NavigateUrl = "";
mnuTestChild.Text = "Child Test";
mnuTest.ChildItems.Add(mnuTestChild);
Menu1.Items.Add(mnuTestChild);*/
                    }
                }
        
              
        }/*
        private void bindmenu()
        {
                       
            string strParam = Request.QueryString["menuid"] + "";
            String strConnString = ConfigurationManager.ConnectionStrings["ROMAConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            con.Open(); 
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd1 = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

                cmd.CommandType = CommandType.Text;
                //cmd1.CommandType = CommandType.Text;
               
                 // cmd.CommandText = " select menu_id from Menu_IDCODE where AccessRights in ('" + strParam + "')";
                //  cmd1.CommandText = "select SubMenu_id from SubmenuRec where Menu_id=" + Session["strParam"].ToString();


                cmd.CommandText = "select * from menu_master ";
                cmd.Connection = con;   
              //  cmd1.Connection = con;

                sda.SelectCommand = cmd;
             //   sda.SelectCommand = cmd1;
                sda.Fill(ds);
                dt = ds.Tables[0];
                DataRow[] drowparent = dt.Select("Menu_id=" + 0);
                foreach (DataRow dr in drowparent)
                { 
                    Menu1.Items.Add( new MenuItem(dr ["menu_name"].ToString()));
                  
                }    
            foreach( DataRow dr in dt.Select("ParentId =" + 0))
            {
                   MenuItem men1 = new MenuItem(dr ["menu_name"].ToString());
                   Menu1.FindItem(dr ["ParentId"].ToString()).ChildItems.Add(men1);
                    

            }
        */      
        
             }

          }
Posted
Updated 17-May-17 2:30am
v2
Comments

1 solution

Instead of using Menu Control, you should use Repeater Control. So that you can use CSS and JS as well.

Save your menu detail in table. Retrieve data and bind Repeater Control line DataTable.

<asp:Repeater ID="RepterDetails" runat="server">

    <HeaderTemplate>
	Menu
    </HeaderTemplate>

    <ItemTemplate>    
	<li> <a href='<%#Eval("link") %>'> <%#Eval("LinkText") %> </a> </li>
    </ItemTemplate>

    <FooterTemplate>
    </FooterTemplate>

</asp:Repeater>
 
Share this answer
 

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