Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dynamically bind menus in master page with the help of database.please help me thanks in advance
Posted
Updated 23-Sep-12 23:46pm
v2
Comments
bhagirathimfs 24-Sep-12 5:34am    
please explain briefly with some example...
[no name] 24-Sep-12 5:44am    
@bhagirathimfs:if i know briefly then why i m asking you?
Devang Vaja 24-Sep-12 6:11am    
Hi deepu pehchana????
http://codemyne.net/articles/Dynamic-Menu-using-Database-in-aspdotnet.aspx?visitid=25&type=2
use this sollution....
[no name] 24-Sep-12 6:35am    
@devang:no who r u
Devang Vaja 24-Sep-12 8:03am    
Deepu yar ye padho<<<<<< http://www.codeproject.com/Answers/451964/i-have-two-grid-view-controls-one-is-temporary-and?cmt=321750#answer1 >>>>>>>>>>>>tumhe khud pata chal jayega fir bat karna deepu ji

Follow the steps below:
Getting the Menu Data From database:
C#
private DataTable GetMenuData()
{
    try
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MenuWithCustomPrivs"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("spMenuItem", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserID", "K010");
            //K010 : Here I hardcoded the UserID. You can set your session's value UserID
            DataTable dtMenuItems = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dtMenuItems);
            cmd.Dispose();
            sda.Dispose();
            return dtMenuItems;
        }
    }
    catch (Exception ex) {
        //Show the error massage here
    }
    return null;
}

Adding the top/parent menu items:
C#
private void AddTopMenuItems(DataTable menuData)
{
      DataView view = null;
      try
      {
          view = new DataView(menuData);
          view.RowFilter = "ParentID IS NULL";
          foreach (DataRowView row in view)
          {
               //Adding the menu item
               MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString());
               menuBar.Items.Add(newMenuItem);
               AddChildMenuItems(menuData, newMenuItem);
          }
      }
      catch (Exception ex)
      {
          //Show the error massage here
      }
      finally {
            view = null;
      }
}

Adding child menu items:
C#
private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
{
    DataView view = null;
    try
    {
        view = new DataView(menuData);
        view.RowFilter = "ParentID=" + parentMenuItem.Value;
        foreach (DataRowView row in view)
        {
            MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString());
            newMenuItem.NavigateUrl = row["NavigateUrl"].ToString();
            parentMenuItem.ChildItems.Add(newMenuItem);
            // This code is used to recursively add child menu items filtering by ParentID
            AddChildMenuItems(menuData, newMenuItem);
        }
    }
    catch (Exception ex)
    {
        //Show the error massage here
    }
    finally
    {
        view = null;
    }
}


Hope it helps.!
--Amit
 
Share this answer
 
Comments
[no name] 24-Sep-12 6:36am    
can you please tell me briefly?
_Amy 24-Sep-12 6:46am    
You'll get the data from database to bind menu, correct? If yes then bind top/parent menu and then try adding the child menu in that. I am writing an article on this. Which you'll get soon on CP.

--Amit
[no name] 24-Sep-12 6:48am    
sir i m asking on the source code
_Amy 24-Sep-12 6:58am    
What you are not able to understand in this code? Every thing is very-very clear. You'll get a datatable of menu items from 1st function. The datatable will contain:
MenuID, NavigateURL, Text and ParentID columns. If the parent id is null for the particular menu then that menu item will be parent item, which I am binding in second function. If that parent item is having any child item then I am calling third function to bind the child items. Now is it clear?

--Amit
[no name] 24-Sep-12 7:25am    
i understand sir m asking about this




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="menu" DataSourceID="xmlDataSource" runat="server" BackColor="#FFFBD6"
DynamicHorizontalOffset="2" Font-Names="Verdana" ForeColor="#990000" StaticSubMenuIndent="10px"
StaticDisplayLevels="1">
<databindings>

<staticselectedstyle backcolor="#FFCC66">
<staticmenuitemstyle horizontalpadding="5px" verticalpadding="2px">
<dynamicmenustyle backcolor="#FFFBD6">
<dynamicselectedstyle backcolor="#FFCC66">
<dynamicmenuitemstyle horizontalpadding="5px" verticalpadding="2px">
<dynamichoverstyle backcolor="#990000" font-bold="False" forecolor="White">
<statichoverstyle backcolor="#990000" font-bold="False" forecolor="White">

<asp:XmlDataSource ID="xmlDataSource" runat="server" TransformFile="~/TransformXSLT.xsl"
XPath="MenuItems/MenuItem">
</div>

</form>
</body>
</html>

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