在repeater的多重嵌套使用中,在前台与label的配合使用会使对reapter元素的遍历变得更为简便!
<asp:Label ID="lbdep" runat="server" Text='<%#Eval("DriveMonthPlanDepartmentID") %> ' Visible="false"></asp:Label>是神来之笔,在后台程序对reapter元素遍历中只需读取label的值,而无需重新读取数据库的repeater绑定值并重新遍历。
repeater前台主要代码:
<table border="1" cellpadding="0" cellspacing="0" style=" margin-bottom: 2px" width="90%" align="center" >
<asp:Repeater runat="server" ID="rpdeplist" OnItemDataBound="rpdeplist_ItemDataBound">
<ItemTemplate>
<tr align="center">
<td class="sctdcontent" >
<%#Eval("DepName")%>
<asp:Label ID="lbdep" runat="server" Text='<%#Eval("DriveMonthPlanDepartmentID") %> ' Visible="false"></asp:Label>
</td>
<td class="sctdcontent">
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<asp:Repeater runat="server" ID="rphdlist" OnItemDataBound="rphdlist_ItemDataBound">
<ItemTemplate>
<tr align="center" >
<td class="sctdcontent" width="50%">
<%#Eval("HangdaoName")%>
<asp:Label ID="lbhangdao" runat="server" Text='<%#Eval("DriveMonthPlanHangdaoID") %> ' Visible="false"></asp:Label>
</td>
<td class="sctdcontent">
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<asp:Repeater runat="server" ID="rpbclist">
<ItemTemplate>
<tr align="center"><td class="sctdcontent">
<%#Eval("_38Type")%> <asp:Label ID="lbbanci" runat="server" Text='<%#Eval("_38ID") %> ' Visible="false"></asp:Label>
</td> <td class="sctdcontent">
<cc2:CdzTextBox ID="txb_drive" runat="server" AllowEmpty="False" Width="150px" ></cc2:CdzTextBox>
</td>
<td class="sctdcontent">
<cc2:CdzTextBox ID="txb_kaituohangdao" runat="server" AllowEmpty="False" Width="150px" ></cc2:CdzTextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td></tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
后台CS主要代码:
private void RpdepBind()
{//GetQuestionTypeAndCount() 返回一个datatable
DateTime day = Convert.ToDateTime(txbDay.Text);
this.rpdeplist.DataSource =new jjjcManage_BLL().GetDeplistFromDMP(day);
this.rpdeplist.DataBind();
if (rpdeplist.Items.Count == 0)
{
palnulldate.Visible = true;
}
else
{
palnulldate.Visible = false;
}
}
protected void rpdeplist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate)
DateTime day = Convert.ToDateTime(txbDay.Text);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
rephangdao = e.Item.FindControl("rphdlist") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
int depid = Convert.ToInt32(rowv["DriveMonthPlanDepartmentID"]); //获取填充子类的id
rephangdao.DataSource = new jjjcManage_BLL().GetHdlistFromDMP(day, depid);
rephangdao.DataBind();
}
}
protected void rphdlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate)
DateTime day = Convert.ToDateTime(txbDay.Text);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
repbanci = e.Item.FindControl("rpbclist") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
// int depid = Convert.ToInt32(rowv["DriveMonthPlanHangdaoID"]); //获取填充子类的id
repbanci.DataSource = new jjjcManage_BLL().GetBanciList();
repbanci.DataBind();
}
}
public void AddorUpdateFuns()
{
DateTime day = Convert.ToDateTime(txbDay.Text);
DataTable dtDep = new jjjcManage_BLL().GetDeplistFromDMP(day);
for (int i = 0; i < dtDep.Rows.Count; i++)
{
int depid = Convert.ToInt32(dtDep.Rows[i][3].ToString());//单位ID
Repeater rephangdao = (Repeater)rpdeplist.Items[i].FindControl("rphdlist");
for (int j = 0; j < rephangdao.Items.Count; j++)
{
int hangdaoid = Convert.ToInt32(((Label)rephangdao.Items[j].FindControl("lbhangdao")).Text.Trim());
Repeater repbanci = (Repeater)rephangdao.Items[j].FindControl("rpbclist");
for (int k = 0; k < repbanci.Items.Count; k++)
{
int banciid = Convert.ToInt32(((Label)repbanci.Items[k].FindControl("lbbanci")).Text.Trim());
TextBox txbdriveout = (TextBox)repbanci.Items[k].FindControl("txb_drive");
TextBox txbdrivekaituo = (TextBox)repbanci.Items[k].FindControl("txb_kaituohangdao");
DataTable dtDrwc = new jjjcManage_BLL().GetDrivelist(day, depid, hangdaoid, banciid);
if (queryflag)
{
if (dtDrwc.Rows.Count != 0)
{
txbdriveout.Text = dtDrwc.Rows[0]["DriveOutput"].ToString();
txbdrivekaituo.Text = dtDrwc.Rows[0]["DriveKaituohangdao"].ToString();
}
else
{
txbdriveout.Text = "0.00";
txbdrivekaituo.Text = "0.00";
}
}
if (btnclickflag)
{
TextBox txbdriveoutnew = (TextBox)repbanci.Items[k].FindControl("txb_drive");
TextBox txbkaituonew = (TextBox)repbanci.Items[k].FindControl("txb_kaituohangdao");
decimal dangriwangcheng = Convert.ToDecimal(txbdriveoutnew.Text);
decimal kaituohangdao = Convert.ToDecimal(txbkaituonew.Text);
bool res = new jjjcManage_BLL().InsertOrUpdateDriveInformation(day, depid, hangdaoid, banciid, dangriwangcheng,kaituohangdao);
if (res)
{
JScript.Alert("信息修改成功");
Response.Write("<script>location.href='jjjcManage1.aspx';</script>");
//?mid=" + Request.QueryString["mid"].ToString() + "
}
else
{
JScript.Alert("信息修改失败");
}
}
}
}
}
}
三层reapter嵌套的功能界面图如下所示: