Okay , below is the code for populating menu items dynamically from a database table. now what i want to do is to save the "PageHeader" from the database into sessions states so that i can use those session values to check the authorization of a user on Page-load of different content pages. now since every user have different no. of authorized pages so the no. of sessions will vary from user to user. the values of those sessions will be matched with a PageHeader variable on the Page-Load of content pages.
private void GetMenu()
{
DataTable dt = new DataTable();
dt = bll.master_Menu_Bar(en);
DataRow[] drow = dt.Select();
foreach (DataRow dr in drow)
{
MenuBar.Items.Add(new MenuItem(dr["PageHeader"].ToString(), dr["PageId"].ToString(), "", dr["PageUrl"].ToString()));
}
business logic layer method used in above code is:
public DataTable master_Menu_Bar(EntityLayer.Entity en)
{
return dll.ReturnSingleTable("Select PageHeader,PageUrl from authorized_view where Emp_Mobile="+ en.cal_EmpMobile);
}
here is my simple code which does the thing ..
private void GetMenu()
{
//fetches data from business logic layer
DataTable dt1 = new DataTable();
dt1 = bll.Master_Menu_Bar(en);
//session so that the masterpage doesnt interact with database on everypostback
Session["dataTable"] = dt1;
DataTable dt=new DataTable();
dt=(DataTable)Session["dataTable"];
//session for page id's of the menuitems which will be checked for authorization at page loads of every page.
int i = 0;
while (i < dt.Rows.Count)
{
int[] PageId = new int[dt.Rows.Count];
PageId[i] = Convert.ToInt32(dt.Rows[i][2]);
Session["PageId" + i] = PageId[i];
i++;
}
//A session to keep count of the no. of menu items , this session is also used at page loads of pages as condition of if statement
Session["count"] = dt.Rows.Count;
DataRow[] drow = dt.Select();
foreach (DataRow dr in drow)
{
MenuBar.Items.Add(new MenuItem(dr["PageHeader"].ToString(), dr["PageId"].ToString(), "", dr["PageUrl"].ToString()));
}