asp.net中Repeater中巢狀Repeater來顯示跟外層Repeater資料相關的其他資訊
阿新 • • 發佈:2019-02-03
在實際開發中,比如在統一許可權管理系統中,我們預先定義了一些列角色,我們點選每一個角色,進入一個應用系統列表,我們要知道每個應用系統中,該角色已經擁有多少選單訪問許可權,我們就可以通過Repeater中巢狀Repeater來顯示這些資訊,如下圖所示:
||
||
||
||
主要程式碼如下:
<table id="table1" class="grid" singleselect="true"> <thead> <tr> <td style="width: 40px; text-align: left;"> <label id="checkAllOff" onclick="CheckAllLine()" title="全選"> </label> </td> <td style="text-align: center;">應用系統名稱 </td> <td style="text-align: center;">應用系統地址 </td> <td style="text-align: center;">角色許可權分配情況 </td> </tr> </thead> <tbody> <asp:Repeater ID="rp_Item" runat="server" OnItemDataBound="rp_Item_ItemDataBound"> <ItemTemplate> <tr> <td style="width: 40px; text-align: left;"> <input type="checkbox" value="<%#Eval("System_ID")%>|<%#Eval("System_Name")%>" name="checkbox" /> </td> <td style="text-align: center;"> <%#Eval("System_Name")%> </td> <td style="text-align: center;"> <%#Eval("System_Url")%> </td> <td style="text-align: center;"> <asp:Repeater ID="rp_Use" runat="server"> <ItemTemplate> <%#((AT.Web.ATBase.SysRole.cMsg)Container.DataItem).msg %> </ItemTemplate> </asp:Repeater> </td> </tr> </ItemTemplate> <FooterTemplate> <% if (rp_Item != null) { if (rp_Item.Items.Count == 0) { Response.Write("<tr><td colspan='4' style='color:red;text-align:center'>沒有找到您要的相關資料!</td></tr>"); } } %> </FooterTemplate> </asp:Repeater> </tbody> </table>
public string _Roles_Name; public string _key; private AT_System_IDAO system_idao = new AT_System_Dal(); protected void Page_Load(object sender, EventArgs e) { if (!base.IsPostBack) { this._Roles_Name = base.Server.UrlDecode(base.Request["Roles_Name"]); this._key = base.Request["key"]; this.InitData(); } } /// <summary> /// 初始化繫結列表資料 /// </summary> private void InitData() { DataTable dt = this.system_idao.GetApplicationList(); ControlBindHelper.BindRepeaterList(dt, this.rp_Item); } protected void rp_Item_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rpt2 = (System.Web.UI.WebControls.Repeater)e.Item.FindControl("rp_Use"); DataRowView rowv = (DataRowView)e.Item.DataItem;//找到外層Repeater關聯的資料項 string systemId = rowv["System_ID"].ToString(); DataTable dtList = this.system_idao.GetMenuBindBySystemId(systemId); DataTable dtRoleRight = this.system_idao.InitRoleRight(this._key, systemId); string msg = "已分配<font color='blue'>" + dtRoleRight.Rows.Count + "</font>/共<font color='red'>" + dtList.Rows.Count + "</font>個選單!"; List<cMsg> result = new List<cMsg>(); cMsg item = new cMsg(); item.msg = msg; result.Add(item); if (result != null) { rpt2.DataSource = result; rpt2.DataBind(); } } }