How to bind Asp menucontrol from database
If you are using asp menucontrol which is not static means it should be bind from database.I will give a best way to bind menucontols having menues and sub menus.
menuControl
<asp:Menu ID="Menu1" runat="server" DynamicVerticalOffset="1" Width="400px" Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False" Style="margin-left: 0px" DynamicPopOutImageTextFormatString=""
StaticPopOutImageTextFormatString="" StaticSubMenuIndent="" StaticMenuItemStyle-ItemSpacing="5px"
StaticMenuItemStyle-Font-Size="Larger">
<StaticMenuStyle />
<StaticMenuItemStyle Height="15px" BorderStyle="Solid" BorderWidth="1px" BorderColor="#164D86"
CssClass="slidetabsmenu" />
<DynamicSelectedStyle CssClass="itemsub" Height="15px" ForeColor="White" />
<DynamicMenuItemStyle CssClass="itemsub" BorderColor="#164D86" Height="15px" BorderStyle="solid"
BorderWidth="1px" ForeColor="White" />
<DynamicHoverStyle CssClass="itemsub_h" Height="15px" ForeColor="White" />
<DynamicMenuStyle Height="15px" ForeColor="White" />
<DynamicItemTemplate>
<asp:Label ID="Label2" Height="15px" runat="server" Text='<%# Eval( "Text" ) %>'
ForeColor="White"> </asp:Label>
</DynamicItemTemplate>
<StaticHoverStyle />
</asp:Menu>
Css Class used(optional)
<style type="text/css">
.itemsub
{
background-color: #77A1B9;
color: #FFFFFF;
padding:1px 2px 1px 2px;
border:none;
margin:0px;
cursor:hand;
}
.itemsub_h
{
background-color: #006699;
color:#FFFFFF;
padding:1px 2px 1px 2px;
border:none;
margin:0px;
}
</style>
first step:
create a table those contain paent element
Example:
create table Menu1
having coloumns
pk_id for primary key,
main_menu for parentmenutext(varchar),
has_children for whether it contains child(bool)
data :pkid main_menu has_children
1 Department true
second step:
create table for submenu
Example:table Menu2
having columns
pk_id for primary key,
mnu1_fk is the foreign key of menu1,
main_menu for submenutext(varchar),
has_children for whether it contains child(bool)//this is require for 3rd lable(optional)
data :pkid parentmenutext mnu1_fk has_children
1 physics 1(pkid of menu1) false(true if maximum label 3)
Code
pageload: if (!Page.IsPostBack)
{
PopulaterRootmenus();
}
bind parentlabel menu from menu1
private void PopulaterRootmenus()
{
string sql = "select main_menu,pk_id,has_children from Menu1";
SqlConnection conn = new SqlConnection("connection string");
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
MenuItem mnuLinkItem = new MenuItem();
mnuLinkItem.Text = Convert.ToString(dt.Rows[i]["main_menu"]);
mnuLinkItem.Value = Convert.ToString(dt.Rows[i]["pk_id"]);
int has_children = Convert.ToInt32(dt.Rows[i]["has_children"]);
Menu1.Items.Add(mnuLinkItem);//add menu
mnuLinkItem.Text = mnuLinkItem.Text;
if (has_children == 1)//if menu has child then bind submenu
{
Populatesecondlevelmenus(mnuLinkItem, mnuLinkItem.Value);
}
}
}
bind submnues
//find submenus of menu and bind under menu
private void Populatesecondlevelmenus(MenuItem mnuLinkItem, string parentVal)
{
string sql = "select pk_id,menu_name,has_children from Menu2 where mnu1_fk = " + Convert.ToInt32(parentVal) + " ";
SqlConnection conn = new SqlConnection("connection string");
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
MenuItem mnuLinkItemsub = new MenuItem();
mnuLinkItemsub.Value = Convert.ToString(dt.Rows[i]["pk_id"]);
mnuLinkItemsub.Text = Convert.ToString(dt.Rows[i]["menu_name"]);
int has_children = Convert.ToInt32(dt.Rows[i]["has_children"]);
mnuLinkItemsub.NavigateUrl = Request.Url.Scheme + "://" + Request.Url.Host + Request.ApplicationPath +"WebForm1.aspx";//set url
mnuLinkItemsub.Text = "<font color = white>" + mnuLinkItemsub.Text + "</font>";//set text and font
mnuLinkItem.ChildItems.Add(mnuLinkItemsub);
}
}
You want submenues under submenu then just add another table like menu2 keep pkid of menu2 as foreign key of menu3 table and set has_childer column of menu2 true which contain submenu.