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
- <asp:repeater id="Rptmenus" runat="server "
- onitemdatabound="Rptmenus_itemdatabound">
- <headertemplate>
- <ul class="mnuLevel01">
- <li id="Mnuhome"><a href="#"> Home </a> </li>
- </headertemplate>
- <ItemTemplate>
- <li class="Multiitem">
- <a target= "<% #DataBinder. Eval (Container.DataItem," target")%>" href= "<% #DataBinder. Eval (Container.DataItem, "Url")%> "><% #DataBinder. Eval (Container.DataItem," Title ")%></a>
- <asp:repeater id="Rptsubmenus" runat= "server">
- <headertemplate>
- <ul class="mnuLevel02">
- </headertemplate>
- <ItemTemplate>
- <li> <a href= "<%# DataBinder.Eval (Container.DataItem, "Url") %> "><% #DataBinder. Eval (Container.DataItem, "Title") %>a></li >
- </ItemTemplate>
- <footertemplate>
- </ul>
- </footertemplate>
- </asp:repeater>
- </li>
- </ItemTemplate>
- <footertemplate>
- </ul>
- </footertemplate>
- </asp:repeater>
CS Code:
- Private DataSet dsmenus;//Menu
- protected void Page_Load (object sender, EventArgs e)
- {
- Binding Main Menu
- String connstr = "Data source=ip address; Initial catalog= database name; User id=sa;password= password; ";
- SqlConnection conn = new SqlConnection (CONNSTR);
- Conn. Open ();
- String cmdtext = "SELECT * from Ep_menus WHERE status=1 ORDER by OrderID asc,id ASC";
- SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
- SqlDataAdapter adapter = New SqlDataAdapter (Cmdtext, connstr);
- Dsmenus = new DataSet ();
- Adapter. Fill (Dsmenus);
- DataView Dvtopmenus = new DataView (Dsmenus.tables[0]);
- dvtopmenus.rowfilter = "[parent]=0";
- Rptmenus.datasource = dvtopmenus;
- Rptmenus.databind ();
- }
- protected void Rptmenus_itemdatabound (object sender, RepeaterItemEventArgs e)
- {
- if (e.item.itemtype = = ListItemType.Item | | E.item.itemtype = = ListItemType.AlternatingItem)
- {
- Repeater Rptsmenus = (Repeater) e.item.findcontrol ("Rptsubmenus");
- DataRowView DRV = (DataRowView) e.Item.DataItem;
- int Pmenu = Convert.ToInt32 (drv["id"]);//parent menu ID
- if (Pmenu! = 0)
- //{
- DataView Dvsubmenus = new DataView (Dsmenus.tables[0]);
- dvsubmenus.rowfilter = "[parent]=" + pmenu;
- if (Dvsubmenus.count > 0)
- {
- Rptsmenus.datasource = dvsubmenus;
- Rptsmenus.databind ();
- }
- //}
- }
- }
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