asp.net中使用Repeater嵌套,提交选中的checkbox项。
主要代码,已测试通过。
前台:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<ul>
<li>
<%#Eval("DepId")%>
<%#Eval("Name")%></li></ul>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" Text='<%#Eval("RealName")%>' />
<asp:HiddenField ID="hidId" Value='<%#Eval("UserId")%>' runat="server" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CF.Model.Department modelDepartment = new CF.Model.Department();
CF.BLL.Department bllDepartment = new CF.BLL.Department();
DataSet ds = bllDepartment.GetAllList();
Repeater1.DataSource = ds.Tables[0].DefaultView;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rp = (Repeater)e.Item.FindControl("Repeater2");
string id = DataBinder.Eval(e.Item.DataItem, "DepId").ToString(); //取得部门id
//根据当前部门的id从人员表中查找员工的数据
CF.Model.Users modelUsers = new CF.Model.Users();
CF.BLL.Users bllUsers = new CF.BLL.Users();
DataSet dsUsers = bllUsers.GetList("DepId=" + id);
rp.DataSource = dsUsers.Tables[0].DefaultView;
rp.DataBind();
}
}
//提交培训名单
protected void btnSubmit_Click(object sender, EventArgs e)
{
string trainPersonsId="";
string trainPersonsNames="";
for (int i = 0; i < this.Repeater1.Items.Count; i++)
{
Repeater rptPersons = (Repeater)this.Repeater1.Items[i].FindControl("Repeater2");
for (int j = 0; j < rptPersons.Items.Count; j++)
{
int id = Convert.ToInt32(((HiddenField)rptPersons.Items[j].FindControl("hidId")).Value);
CheckBox cb = (CheckBox)rptPersons.Items[j].FindControl("cb");
if (cb.Checked)
{
trainPersonsId += id + ",";
trainPersonsNames += cb.Text.Trim() + ",";
}
}
//foreach (RepeaterItem item in rptPersons.Items)
//{
// if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
// {
// CheckBox cb = item.FindControl("cb") as CheckBox;
// if (cb.Checked)
// {
// trainPersonsNames += cb.Text + "|";
// }
// }
}
&
补充:Web开发 , ASP.Net ,