Wednesday, February 4, 2009

how to Generate the Menu(s) programitically in asp.net and c#.net

How to Build Menu’s dynamically with Server Coding only in asp.net and c#.net

Hi to all,

After long time, I got time to post the article into this blog.

Creating the Menu’s in asp.net is biggest challenge. It is possible by two ways
1. Static Menu generation(design the menu component at the time of page designing)
2. Dynamic Menu Build.( the menu components are designed at the time of fly)

Second technic is more helpful than first one: Normally, in any project/application navigations are done by menucontrol. The number of menuitems are applicapable for users is depend upon their rights. Admin can able to see the all of the menuitems from the application.

These are the some good points for, how to collecting the menuitems.

1. The number of menuitems for particular user is depend upon his rights. So, at the time of login itself it’s better to find out the menuitems collections for respective user. So, it’s better to maintain the “detailedMenu” table for holding menu items with respective of the users. It’s better to maintain one more column for parent/child relation for the menu’s. This column specify’s the which child is correspond to which parent.

2. It’s better to keep the “detailedMenu” table structure as follows:
(it’s only for sample, I am putting only mandatory fields. Remaining things are depend upon your requirement)

DetaildID(it’s unique)
Int
UserID
Int
MenuCode
int

2. Name of the menuitems, link informations are held in the menu table as follows:
(it’s only for sample, I am putting only mandatory fields. Remaining things are depend upon your requirement)
MenuCode (it’s unique)
Int
MenuType(it’s specify’s the parent/child)
Int
MenuName(menuitem which is appear in the application)
Varchar(50)
MenuLink(navigate page name .aspx, .html)
Varchar(80)


So, there is a realation between above tables.


Implementation(coding):
At the time of login to the application, the number of menuItems are find from the “detailedMenu” table on the basis of UserID.
So, collect the appropriate elements from two tables.

Put this collection information into the DataSet for programming.

Take one UserControl and put that user control into the master page:
In the usercontrol page_load write the following code

/* this is the code for Webusercontrol page Load */
protected void Page_Load(object sender, EventArgs e)
{


sqlAdp.Fill(ds, "Menu");
strMenu = GenerateUL(ds);
if (strMenu != null)
{

MenuDiv.InnerHtml = strMenu;
string JavaScript = "";
Page.RegisterStartupScript("js", JavaScript);
}
else
{
StringBuilder strError = new StringBuilder();
strError.Append("");
strError.Append("!!! Menu Could not be generated

");
strError.Append("You don’t have proper Menu Righs");
strError.Append("Please kindly contact to Administration if this error prevails");
MenuDiv.InnerHtml = strError.ToString();
}




}

private string GenerateUL(DataSet ds)
{
StringBuilder strMenu = new StringBuilder("");
return strMenu.ToString();
}


It’s give proper idea to build the MenuItems by Server coding. No need to write much things at client side script. I hope this article is helping for you. I happy to accept suggestions/comments/corrections. I will be reached at subhas.soft@gmail.com.

Next time meet with better scenario. Happy Coding.

Cheers,
Subhas