Sunday 10 November 2013

menu submenu using Database in asp.net

menu submenu using Database in asp.net :




aspx :

 <style type="text/css">

        .MenuUL {
            list-style: none;
            padding: 0px;
            margin: 0px;
        }

        .MenuUL li {
                display: block;
                position: relative;
                float: left;
                border: 1px solid #000;
            }

       li ul {
            display: none;
        }

        ul li a {
            display: block;
            background: #000;
            padding: 5px 10px 5px 10px;
            text-decoration: none;
            white-space: nowrap;
            color: #fff;
        }

            ul li a:hover {
                background: #f00;
            }

        li:hover ul {
            display: block;
            position: absolute;
        }

        li:hover li {
            float: none;
        }

        li:hover a {
            background: #f00;
        }

        li:hover li a:hover {
            background: #000;
        }

        #drop-nav li ul li {
            border-top: 0px;
        }
    </style>

<div>
       <ul id="drop-nav " class="MenuUL">
// Repeater Control :
            <asp:Repeater ID="rptMenu" runat="server"        
                            OnItemCommand="rpt_ItemCommand"               
                           OnItemDataBound="rpt_ItemDataBound">
             <ItemTemplate>
                    <li>
                         <a href="#">
                            <asp:Label ID="lblMenu1" CssClass="lblMenu"              
                                   runat="server" Text='<%#Eval("Menu") %>'>       
                              </asp:Label>
                          </a>
                     <ul>
// Repeater Control SubMenu :
            <asp:Repeater ID="rptSub" runat="server">
                 <ItemTemplate>
                        <li>
                             <a href="#">
                                  <asp:Label ID="lblSubMenu1" CssClass="lblMenu"                                           runat="server" Text='<%#Eval("SubMenu") %>'>
                                  </asp:Label>
                                </a>
                    </li>
                 </ItemTemplate>
              </asp:Repeater>
                  </ul>
              </li>
       </ItemTemplate>
  </asp:Repeater>
            </ul>
        </div>

aspx:cs:

SqlConnection objConn = new             
                 SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        SqlDataAdapter objDa = new SqlDataAdapter();

 protected void Page_Load(object sender, EventArgs e)
        {
            ViewMenu();
        }
 public void ViewMenu()
        {
           string Select;
           Select = "SELECT  * FROM Menu ORDER BY ID Desc; SELECT * FROM  
                         SubMenu SM INNER JOIN Menu M ON M.ID=SM.SubMenuID ";
            objDa = new SqlDataAdapter(Select, objConn);
            objDa.Fill(ds);
            ds.Relations.Add(new DataRelation("Menu",          
                             ds.Tables[0].Columns["ID"],                   
                             ds.Tables[1].Columns["SubMenuID"]));
               rptMenu.DataSource = ds.Tables[0];
            rptMenu.DataBind();
        }

        protected void rptMenu_ItemCommand(object source,      
                                                     RepeaterCommandEventArgs e)
        {
            Repeater rptSubMenu = e.Item.FindControl("rptSubMenu") 
                                                                                       as Repeater;
            rptSubMenu.DataSource = ds.Tables[1];
            rptSubMenu.DataBind();

        }

        protected void rpt_ItemCommand(object source,   
                                                       RepeaterCommandEventArgs e)
        {
           
        }

        protected void rpt_ItemDataBound(object sender,             
                                                                  RepeaterItemEventArgs e)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            Repeater rptSubMenu = e.Item.FindControl("rptSub") as Repeater;

            rptSubMenu .DataSource = drv.CreateChildView("Menu");
            rptSubMenu .DataBind();
        }

          





No comments:

Post a Comment