这里的Repeater 嵌套,是指在 外层的Repeater的 ItemTemplate 中 又添加了一个 Repeater,并且在两层的Repeater中都添加了一个Button控件。我们要实现两个控件的事件响应。也就是点两个Button时执行相应的方法。
aspx页面:
<asp:RepeaterID="Repeater1" runat="server"onitemdatabound="Repeater1_ItemDataBound"onitemcommand="Repeater1_ItemCommand">// 外层的Repeater。注意Onitemcommand 事件 <HeaderTemplate></HeaderTemplate><ItemTemplate> <asp:ButtonText="删除" runat="server" CommandName="deletely"CommandArgument='<%# eval_r("ID") %>'/>//这是外层的Button。 <asp:RepeaterID="Repeater2" runat="server"onitemcommand="Repeater2_ItemCommand">//嵌套到里面的第二Repeate <HeaderTemplate> </HeaderTemplate> <ItemTemplate><asp:ButtonID="Button1" Text="删除" runat="server" CommandName="deletehf"CommandArgument='<%# eval_r("ID") %>'/>// 内层的 Button。<asp:CheckBox ID="chkId" CssClass="checkall"runat="server" /><asp:HiddenFieldID="hidId" Value='<%#eval_r("id")%>'runat="server" />//内层实现全选批量操作 </ItemTemplate> <FooterTemplate> </FooterTemplate> </asp:Repeater><FooterTemplate></FooterTemplate></asp:Repeater>
后台代码//对内层repeater进行绑定赋值protected void Repeater1_ItemDataBound(objectsender, RepeaterItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem) { Repeater rep =e.Item.FindControl(" Repeater2") asRepeater; DataRowView rowv =(DataRowView)e.Item.DataItem; int typeid =Convert.ToInt32(rowv["id"]); //获取填充子类的id rep.DataSource = //获取绑定数据; rep.DataBind(); } }//处理第1个Button事件,protected voidRepeater1_ItemCommand(object source, RepeaterCommandEventArgse){ if (e.CommandName == "deletely") { int dlid =Convert.ToInt32(e.CommandArgument.ToString()); //执行方法//执行过后绑定}}//处理第2个Button事件。 protected voidRepeater2_ItemCommand(object source, RepeaterCommandEventArgse){if (e.CommandName == "deletehf"){ int dlid =Convert.ToInt32(e.CommandArgument.ToString()); //执行方法 //执行过后绑定}}//批量删除 protected void btnDelete_Click(object sender,EventArgs e) { for (int i= 0; i < Repeater1.Items.Count;i++) { Repeater rep= Repeater1.Items[i].FindControl("Repeater2") asRepeater; for (int j = 0; j< rep.Items.Count; j++) { int id =Convert.ToInt32(((HiddenField)rep.Items[j].FindControl("hidId")).Value); CheckBox cb =(CheckBox)rep.Items[j].FindControl("chkId");
if (cb.Checked) { //执行方法 } } } //执行过后绑定 }