Asp.Net Repeater的两层嵌套以及遍历取值的简单实现

  <select id="s6" multiple="multiple">
<asp:Repeater ID="rpl_dept" runat="server"
onitemdatabound
="rpl_dept_ItemDataBound">
<ItemTemplate>
<optgroup label="<%#Eval("Name") %>">
<asp:Repeater ID="rpl_user" runat="server">
<ItemTemplate>
<option><%#Eval("Name")%></option>
</ItemTemplate>
</asp:Repeater>
</optgroup>
</ItemTemplate>
</asp:Repeater>
</select>
   protected void rpl_bind()
{
com.hmby.BLL.Depts dept = new com.hmby.BLL.Depts();
string strWhere = "";
DataTable dt = new DataTable();
dt = dept.GetList(strWhere).Tables[0];
if (dt.Rows.Count > 0)
{
this.rpl_dept.DataSource = dt;
this.rpl_dept.DataBind();
}
}

protected void rpl_dept_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rpl_user") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
int deptId = Convert.ToInt32(rowv["Id"]); //获取填充子类的id
DataTable dt = new DataTable();
com.hmby.BLL.Users user = new com.hmby.BLL.Users();
string strWhere = " DeptId=" + deptId;
dt = user.GetList(strWhere).Tables[0]; ;
if (dt.Rows.Count > 0)
{
rep.DataSource = dt;
rep.DataBind();
}

}

}

另外一种嵌套的前台(表格)(下边是遍历)

<asp:Repeater ID="rpl_mParent" runat="server"
                        onitemdatabound="rpl_mParent_ItemDataBound">
    <ItemTemplate>
        <tr>
            <td style="vertical-align:top;background-color:#ffa !important;">
            <input type="checkbox" id="cbox_p" value='<%# Eval("Id")%>' runat="server"/>
      <label class="choice" for="cb1"><%# Eval("ModelName")%></label>
            </td>
        </tr>
        <asp:Repeater ID="rpl_mChild" runat="server">
        <ItemTemplate>
        <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="checkbox" id="cbox_c" value='<%# Eval("Id")%>' runat="server"/>
     <label class="choice" for="cb1"><%# Eval("ModelName")%></label>
        </td></tr>
        </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    </asp:Repeater>

遍历嵌套的Checkbox,双层遍历

for (int i=0;i<this.rpl_mParent.Items.Count;i++)
{
HtmlInputCheckBox cbox_p = (HtmlInputCheckBox)rpl_mParent.Items[i].FindControl("cbox_p");
if (cbox_p.Checked)
{
//选中了checkbox_p
JsUtil.Alert(this, "p");
}
Repeater rplChild = (Repeater)rpl_mParent.Items[i].FindControl("rpl_mChild");
string c = "";
for (int j=0;j<rplChild.Items.Count;j++)
{
HtmlInputCheckBox cbox_c = (HtmlInputCheckBox)rplChild.Items[j].FindControl("cbox_c");
if (cbox_c.Checked)
{
//选中了checkbox_c
c += cbox_c.Value + ",";
}
}
JsUtil.Alert(this, c);
}





原文地址:https://www.cnblogs.com/mushaobai/p/2303660.html