asp:Repeater实例备忘
1.前置部分
<asp:Repeater ID="rptPlanNo" runat="server" OnItemDataBound="rptPlanNo_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:Repeater>
2.1后置代码的数据绑定部分
protected void dplType_SelectedIndexChanged(object sender, EventArgs e)
{
//......
List<PlanNo_Generated_DistributeEntity> list = dal.GetAll(parms, "Id", 30, 0, out recordCount);//30表示控制每次派发个数
rptPlanNo.DataSource = list;
rptPlanNo.DataBind();
}
2.2在绑定Repeater时,怎样拿到ItemTemplate中的asp:CheckBox,并且取得绑定的数据项,如这里的PlanNo_Generated_DistributeEntity
protected void rptPlanNo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
PlanNo_Generated_DistributeEntity entity = e.Item.DataItem as PlanNo_Generated_DistributeEntity;
CheckBox check = e.Item.FindControl("cb") as CheckBox;
check.Text = entity.Plan_No;
}
}
2.3 在提交时,怎样取得ItemTemplate中的asp:CheckBox
protected void btnSendPlanNo_Click(object sender, EventArgs e)
{
//....
foreach (Control c in this.rptPlanNo.Controls)
{
CheckBox cbx = (CheckBox)c.FindControl("cb");
if (cbx != null)
{
if (cbx.Checked == true)
{
entity.Plan_No = cbx.Text;
//....
}
}
}
}