Asp.Net Repeater控件绑定泛型List<T>的用法之绑定二级repeater的方法
前文有提到了如何在list<T>泛型中绑定了repeater的用法,今天在做项目的过程当中,又碰到了repeater的嵌套循环,二级repeater的绑定方法类似,关键点在于Datarow的一个类型转换。
原代码:
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater dataList = e.Item.FindControl("Rpt2") as Repeater;
DataRowView rowv = (DataRowView)e.Item.DataItem;
int DepID = Convert.ToInt32(rowv["DepID"].ToString());
dataList.DataSource = DbHelper.ExecuteTable("select * from CNVP_AdminDep where DepID=" + DepID + " and AdminID in (select AdminID from CNVP_AdminGroup Where GroupID in (17,18,19,20))");
dataList.DataBind();
}
}
现改为
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater dataList = e.Item.FindControl("SubDep") as Repeater;
DepartmentModel rowv = (DepartmentModel)e.Item.DataItem;
int DepID = rowv.DepartID;
string Sql = " 1=1 and ParentID=" + DepID;
DepartmentData bll1 = new DepartmentData();
List<DepartmentModel> model1 = bll1.GetDepartmentList(Sql);
if (model1 != null)
{
dataList.DataSource = model1;
dataList.DataBind();
}
}
}