Akash Patel

Akash Patel

  • NA
  • 117
  • 38.6k

Display Sub-menu Items under main menu

Nov 20 2018 11:19 PM
I am developing a project in which there is a vertical menu on left side. There are main menus alsong with sub menus in which sub menus are retrieved from database. I have used datalist control for menu which also displays its associated sub menus. Suppose for 1st menu there are 3 sub menus. When i run the website all menus display correctly but sub menus, instead of displaying all 3 of them only one sub menu is displayed. Below is the code of datalist.
 
ASPX Code
  1. <div class="left-side">  
  2. <h3 class="agileits-sear-head">  
  3. Category</h3>  
  4. <ul id="accordion" class="accordion">  
  5. <asp:DataList ID="dlCategory" runat="server" RepeatColumns="1" RepeatDirection="Vertical">  
  6. <ItemTemplate>  
  7. <li>  
  8. <div class="link">  
  9. <%#Eval("Category_Name") %><i class="fa fa-chevron-down"></i></div>  
  10. <ul class="submenu">  
  11. <li><asp:HyperLink ID="hypProduct" runat="server" NavigateUrl='<%# GetUrl(Eval("PID"))%>' Text='<%#Eval("Product_Name") %>'> </asp:HyperLink>  
  12. </li>  
  13. </ul>  
  14. </li>  
  15. </ItemTemplate>  
  16. </asp:DataList>  
  17. </ul>  
  18. </div>  
  19. </div>  
C# Code
  1. private void GetAllCategoryAndProducts()  
  2. {  
  3. using (SqlCommand cmd = new SqlCommand("GetCategory", con))  
  4. {  
  5. cmd.CommandType = CommandType.StoredProcedure;  
  6. using (SqlDataAdapter adp = new SqlDataAdapter(cmd))  
  7. {  
  8. ds = new DataSet();  
  9. adp.Fill(ds, "CategoryProduct");  
  10. ds.Tables["CategoryProduct"].Columns.Add("PID"typeof(int));  
  11. ds.Tables["CategoryProduct"].Columns.Add("Product_Name"typeof(string));  
  12. foreach (DataRow dr in ds.Tables["CategoryProduct"].Rows)  
  13. {  
  14. using (SqlCommand cmd2 = new SqlCommand("SELECT PID,Product_Name FROM tblProduct,tblCategory WHERE tblProduct.Category=@cid AND tblProduct.Category=tblCategory.CID", con))  
  15. {  
  16. cmd2.Parameters.AddWithValue("@cid", dr["CID"]);  
  17. con.Open();  
  18. SqlDataReader reader = cmd2.ExecuteReader();  
  19. while (reader.Read())  
  20. {  
  21. dr["PID"] = reader["PID"].ToString();  
  22. dr["Product_Name"] = reader["Product_Name"].ToString();  
  23. }  
  24. reader.Close();  
  25. con.Close();  
  26. }  
  27. }  
  28. }  
  29. dlCategory.DataSource = ds;  
  30. dlCategory.DataBind();  
  31. }  
  32. }

Answers (4)