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/archive/2011/12/27/2303660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值