(ext.) ASP. Repeater and Level two menu

Source: Internet
Author: User

Let's start with a picture that shows the menu we want to implement:

This menu only achieves level 2 ha.

The method I used was to nest 2 repeater.

First look at the table structure in the database:

Data:

On the code:

aspx

  1. <asp:repeater id="Rptmenus" runat="server "
  2. onitemdatabound="Rptmenus_itemdatabound">
  3. <headertemplate>
  4. <ul class="mnuLevel01">
  5. <li id="Mnuhome"><a href="#"> Home </a> </li>
  6. </headertemplate>
  7. <ItemTemplate>
  8. <li class="Multiitem">
  9. <a target= "<% #DataBinder. Eval (Container.DataItem," target")%>" href= "<% #DataBinder. Eval (Container.DataItem, "Url")%> "><% #DataBinder. Eval (Container.DataItem," Title ")%></a>
  10. <asp:repeater id="Rptsubmenus" runat= "server">
  11. <headertemplate>
  12. <ul class="mnuLevel02">
  13. </headertemplate>
  14. <ItemTemplate>
  15.                      <li> <a href= "<%# DataBinder.Eval (Container.DataItem, "Url")  %> "><% #DataBinder. Eval (Container.DataItem, "Title")  %>a></li > 
  16. </ItemTemplate>
  17. <footertemplate>
  18. </ul>
  19. </footertemplate>
  20. </asp:repeater>
  21. </li>
  22. </ItemTemplate>
  23. <footertemplate>
  24. </ul>
  25. </footertemplate>
  26. </asp:repeater>

CS Code:

  1. Private DataSet dsmenus;//Menu
  2. protected void Page_Load (object sender, EventArgs e)
  3. {
  4. Binding Main Menu
  5. String connstr = "Data source=ip address; Initial catalog= database name; User id=sa;password= password; ";
  6. SqlConnection conn = new SqlConnection (CONNSTR);
  7. Conn. Open ();
  8. String cmdtext = "SELECT * from Ep_menus WHERE status=1 ORDER by OrderID asc,id ASC";
  9. SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
  10. SqlDataAdapter adapter = New SqlDataAdapter (Cmdtext, connstr);
  11. Dsmenus = new DataSet ();
  12. Adapter. Fill (Dsmenus);
  13. DataView Dvtopmenus = new DataView (Dsmenus.tables[0]);
  14. dvtopmenus.rowfilter = "[parent]=0";
  15. Rptmenus.datasource = dvtopmenus;
  16. Rptmenus.databind ();
  17. }
  18. protected void Rptmenus_itemdatabound (object sender, RepeaterItemEventArgs e)
  19. {
  20. if (e.item.itemtype = = ListItemType.Item | | E.item.itemtype = = ListItemType.AlternatingItem)
  21. {
  22. Repeater Rptsmenus = (Repeater) e.item.findcontrol ("Rptsubmenus");
  23. DataRowView DRV = (DataRowView) e.Item.DataItem;
  24. int Pmenu = Convert.ToInt32 (drv["id"]);//parent menu ID
  25. if (Pmenu! = 0)
  26. //{
  27. DataView Dvsubmenus = new DataView (Dsmenus.tables[0]);
  28. dvsubmenus.rowfilter = "[parent]=" + pmenu;
  29. if (Dvsubmenus.count > 0)
  30. {
  31. Rptsmenus.datasource = dvsubmenus;
  32. Rptsmenus.databind ();
  33. }
  34. //}
  35. }
  36. }

Here is just a general idea and code, specific repeater and other information please refer to other tutorials.

(ext.) ASP. Repeater and Level two menu

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.