ASP.NET Repeater巢狀Repeater實現選單載入
阿新 • • 發佈:2019-01-28
在KS系統中要實現從資料庫中讀取介面許可權檔案實現選單、介面的動態載入。
效果圖:
ASP.NET介面程式碼
<div id="menu-container"> <asp:Repeater ID="rpMenu" runat="server" onitemdatabound="rpMenu_ItemDataBound"> <ItemTemplate> <div class="menu-tit" id="MenuName"> <asp:HiddenField ID="hfMenuID" runat="server" Value='<%#Eval("Id") %>' /> <%#Eval("MenuName") %></div> <div class="menu-list"> <div class="top-line"> </div> <ul class="nav-items"> <asp:Repeater ID="rpWindows" runat="server"> <ItemTemplate> <li><a href='<%#Eval("WindowsURL") %>' target="content" id="WindwosName"><%#Eval("WindowsName") %></a></li> </ItemTemplate> </asp:Repeater> </ul> </div> </ItemTemplate> </asp:Repeater> </div>
C#介面後臺程式碼
public partial class AdmWelcom : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strLike=""; DataBindMenu(strLike); } public void DataBindMenu(string strLike) { DataTable dtMenu= new DataTable(); AdmMenuManager admMenuManager = new AdmMenuManager(); //查詢選單 dtMenu = admMenuManager.QueryMenu(strLike); rpMenu.DataSource =dtMenu; rpMenu.DataBind(); } protected void rpMenu_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rpWindows = e.Item.FindControl("rpWindows") as Repeater; HiddenField hfMenuID=e.Item .FindControl ("hfMenuID") as HiddenField; DataTable dt = new DataTable(); string strLike = ""; BLL.AdmMenuManager admMenuManager = new AdmMenuManager(); string strMenuID = hfMenuID.Value.ToString().Trim() ; strLike = " TR_MenuWindows.MenuID='" + strMenuID + "'"; //查詢介面下的窗體 dt = admMenuManager.QueryMenuWindows(strLike); rpWindows.DataSource = dt; rpWindows.DataBind(); } } }